Search code examples
phpxmlsimplexml

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


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!


Solution

  • 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