php 如何使用php从url读取xml文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12542469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:46:10  来源:igfitidea点击:

how to read xml file from url using php

phpxmlurl

提问by Asif hhh

I have to read an XML file from an URL

我必须从 URL 读取 XML 文件

$map_url = "http://maps.google.com/maps/api/directions/xml?origin=".$merchant_address_url."&destination=".$customer_address_url."&sensor=false";

This gives me an URL like:

这给了我一个 URL,如:

http://maps.google.com/maps/api/directions/xml?origin=Quentin+Road+Brooklyn%2C+New+York%2C+11234+United+States&destination=550+Madison+Avenue+New+York%2C+New+York%2C+10001+United+States&sensor=false

http://maps.google.com/maps/api/directions/xml?origin=Quentin+Road+Brooklyn%2C+New+York%2C+11234+United+States&destination=550+Madison+Avenue+New+York% 2C+New+York%2C+10001+United+States&sensor=false

I am using this function to read and then get data:

我正在使用此函数读取然后获取数据:

 $response_xml_data = file_get_contents($map_url);
 if($response_xml_data){
     echo "read";
 }

 $data = simplexml_load_string($response_xml_data);
 echo "<pre>"; print_r($data); exit; 

But no luck, any help?

但没有运气,有什么帮助吗?

回答by Prasath Albert

you can get the data from the XML by using "simplexml_load_file" Function. Please refer this link

您可以使用“simplexml_load_file”函数从 XML 中获取数据。请参考这个链接

http://php.net/manual/en/function.simplexml-load-file.php

http://php.net/manual/en/function.simplexml-load-file.php

$url = "http://maps.google.com/maps/api/directions/xml?origin=Quentin+Road+Brooklyn%2C+New+York%2C+11234+United+States&destination=550+Madison+Avenue+New+York%2C+New+York%2C+10001+United+States&sensor=false";
$xml = simplexml_load_file($url);
print_r($xml);

回答by NotGaeL

Your code seems right, check if you have fopen wrappersenabled (allow_url_fopen = Onon php.ini)

您的代码似乎正确,请检查您是否启用了fopen 包装器allow_url_fopen = On在 php.ini 上)

Also, as mentioned by other answers, you should provide a properly encoded URI or encode it using urlencode()function. You should also check if there is any error fetching the XML string and if there is any parsing error, which you can output using libxml_get_errors()as follows:

此外,如其他答案所述,您应该提供正确编码的 URI 或使用urlencode()函数对其进行编码。您还应该检查获取 XML 字符串是否有任何错误以及是否有任何解析错误,您可以使用libxml_get_errors()输出,如下所示:

<?php
if (($response_xml_data = file_get_contents($map_url))===false){
    echo "Error fetching XML\n";
} else {
   libxml_use_internal_errors(true);
   $data = simplexml_load_string($response_xml_data);
   if (!$data) {
       echo "Error loading XML\n";
       foreach(libxml_get_errors() as $error) {
           echo "\t", $error->message;
       }
   } else {
      print_r($data);
   }
}
?>

If the problem is you can't fetch the XML code maybe it's because you need to include some custom headers in your request, check how to use stream_context_create()to create a custom stream context for use when calling file_get_contents()on example 4 at http://php.net/manual/en/function.file-get-contents.php

如果问题是您无法获取 XML 代码,可能是因为您需要在请求中包含一些自定义标头,请检查如何使用stream_context_create()创建自定义流上下文,以便file_get_contents()http:/调用示例 4 时使用/php.net/manual/en/function.file-get-contents.php

回答by Doglas

file_get_contents()usually has permission issues. To avoid them, use:

file_get_contents()通常有权限问题。要避免它们,请使用:

function get_xml_from_url($url){
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');

    $xmlstr = curl_exec($ch);
    curl_close($ch);

    return $xmlstr;
}

Example:

例子:

$xmlstr = get_xml_from_url('http://www.camara.gov.br/SitCamaraWS/Deputados.asmx/ObterDeputados');
$xmlobj = new SimpleXMLElement($xmlstr);
$xmlobj = (array)$xmlobj;//optional

回答by li bing zhao

$url = 'http://www.example.com'; $xml = simpleXML_load_file($url,"SimpleXMLElement",LIBXML_NOCDATA);

$url = 'http://www.example.com'; $xml = simpleXML_load_file($url,"SimpleXMLElement",LIBXML_NOCDATA);

$url can be php file, as long as the file generate xml format data as output.

$url 可以是php文件,只要文件生成xml格式的数据作为输出即可。

回答by Brett Zamir

It is working for me. I think you probably need to use urlencode()on each of the components of $map_url.

它对我有用。我认为您可能需要urlencode()$map_url.