XML 文件在浏览器中打开但无法用 simplexml_load_file 打开

XML file opens in the browser but does not open with simplexml_load_file

当我在浏览器上打开 XML(客户端 2)的 URL 时,它可以完美打开,但是当我尝试打开相同的 URL(完整复制并粘贴) 在 PHP 中使用 simplexml_load_file 我得到了错误,包括 404 (NOT FOUND)。有人有解决办法吗?

注意:我将客户地址 (URL) 和 PHP 文件的路径更改为“/path/arquivo.php”。

<?php

 $customers[1] = 'http://www.customer1.com.br/dados.xml';
 $customers[2] = 'http://www.customer2.com.br/dados.xml'; 

 $xml1 = simplexml_load_file ($customers[1]); // it works perfectly 
 $xml2 = simplexml_load_file ($customers[2]); // ERROR 

我遇到的错误

Warning: file_get_contents (http://www.cliente2.com.br/dados.xml): failed to open stream: HTTP request failed! HTTP / 1.1 404 Not Found in /path/arquivo.php on line 5

Warning: simplexml_load_file (): I / O warning: failed to load external entity "http://www.cliente2.com.br/dados.xml" in /path/arquivo.php on line 5

我在打开文件之前尝试使用 libxml_disable_entity_loader (false);,但错误仍然存​​在。

我正在使用 Ubuntu 14.04.3 LTS,PHP 5.5.9.

我将不胜感激!

非常感谢!

网络服务器似乎过滤掉了没有正确 User-Agent 的请求。

所以你需要使用curl,传递用户代理(我刚刚使用最新的Chrome用户代理)并获得响应。

<?php

$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_URL,'http://www.cliente2.com.br/dados.xml');
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36');
$response = curl_exec($curl_handle);
curl_close($curl_handle);

//var_dump($response);

$xml = simplexml_load_string($response);

print_r($xml);

做好长输出的准备