【问题标题】:how to read xml file from url using php如何使用php从url读取xml文件
【发布时间】:2012-09-14 13:56:40
【问题描述】:

我必须从 URL 读取 XML 文件

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

这给了我一个类似的 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

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

 $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; 

但运气不好,有什么帮助吗?

【问题讨论】:

    标签: php xml url


    【解决方案1】:

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

    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);
    

    【讨论】:

    • 是的,我试过 simplexml_load_file 但是当我 print_r 然后它给了我空白页
    • 确保您在 php.ini 中启用了 allow_url_fopen
    【解决方案2】:

    您的代码似乎正确,请检查您是否启用了fopen wrappers(php.ini 上的allow_url_fopen = On

    此外,正如其他答案所述,您应该提供正确编码的 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);
       }
    }
    ?>
    

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

    【讨论】:

    • 当我使用上面的代码时,只给出“加载 XML 时出错\n”,而不是任何其他消息
    • 那么问题一定出在file_get_contents($map_url)。如果您的 URI 有效且 allow_url_fopen 为 On,则可能是您在调用函数时需要提供自定义上下文...
    • 这将转储大量乱码而不报告错误-因为您在 libxml_get_errors() 上没有 oid
    【解决方案3】:

    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;
    }
    

    例子:

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

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

        【解决方案5】:

        它对我有用。我认为您可能需要在$map_url 的每个组件上使用urlencode()

        【讨论】:

          猜你喜欢
          • 2021-05-05
          • 1970-01-01
          • 2014-02-27
          • 2023-03-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多