1

When I open the URL of XML (client 2) on browser, it opens perfectly, but when I try to open the same URL (copied and pasted in full) with simplexml_load_file in PHP I got the errors, including 404 (NOT FOUND). Does anyone have any solution?

Note: I changed the customers's addresses (URL) and the path of the PHP file to '/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 

Errors that I got

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

I've tried using libxml_disable_entity_loader (false); before opening the file but the error remains.

I'm using Ubuntu 14.04.3 LTS, with PHP 5.5.9.

I'll appreciate any help!

Thanks a lot!

4
  • Are these the actual error messages? ie: is the url given in the errors the actual url? Commented Nov 6, 2015 at 22:12
  • just to confirm, are you able to open both urls in a browser window but one of them gives out a 404 when you try to load it with simplexml_load_file? Can you access the url from a terminal via curl? ex. $> curl http://problem.url.com/dados.xml ? Commented Nov 6, 2015 at 22:17
  • Yes, both works. And Yes, I can access it from a terminal via curl. Commented Nov 6, 2015 at 22:19
  • I updated my question and ADDED a real example. Commented Nov 6, 2015 at 22:22

1 Answer 1

4

It appears that the webserver filters out requests coming in without a proper User-Agent.

So you need to use curl, pass an user agent (I just used the latest Chrome user agent) and get the response.

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

Be prepared for a long output

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot! Works very well!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.