Search code examples
phpxmlsimplexml

SimpleXML_Load_File returns no results for one URL, OK for another


I have a third party service I am querying to get an XML file returned. If I visit that URL in my browser I see the XML data, but the SimpleXML_Load_File just crashes out, and I cannot get it to display any errors either. The first DVDs URL works and loads fine, but the games one does not.

Can anyone see anything wrong with my code here, or with the XML being returned to indicate why the simplexml_load_file would just fail and prevent any more php on the page executing?

<?php
######DVDs
$dvdURL = 'http://dvd.find-services.co.uk/dvdSearch.aspx?sort=popular&site=sample&pagesize=1';

$dvdfeed = simplexml_load_file($dvdURL);
var_dump($dvdfeed);
echo '<hr>';
###########Games
$gameURL = 'http://game.find-services.co.uk/gameSearch.aspx?order=popular&site=sample&pagesize=1';

$gamefeed = simplexml_load_file($gameURL);
var_dump($gamefeed);
?>

OK, after a comment from Grim... about the code working on PHPfiddle, I have used just the code below on my server:

<?php
$gameURL = 'http://game.find-services.co.uk/gameSearch.aspx?order=popular&site=sample&pagesize=1';
$gamefeed = simplexml_load_file($gameURL);
var_dump($gamefeed);
?>

which gives an error!

Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request. Please contact the server administrator at [email protected] to inform them of the time this error occurred, and the actions you performed just before this error. More information about this error may be available in the server error log. Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.

However, visiting the error log in my cPanel shows no entries. Any ideas?

@Grim... suggested using cURL, so here is the new code:

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://game.find-services.co.uk/gameSearch.aspx?order=popular&site=sample&pagesize=1");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$xml = curl_exec($ch);
$xml = utf8_encode($xml);
var_dump($xml);
$simpleXml = simplexml_load_string($xml);
var_dump($simpleXml);
curl_close($ch);
?>

The second var_dump is still blank, and the first gives:

string(541) "B00ZG1S834ps4PlayStation 446927No Mans Sky11148490"

with a view source showing:

string(541) "<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?><games><game><id>B00ZG1S834</id><platform>ps4</platform><platformName>PlayStation 4</platformName><category><![CDATA[Strategy]]></category><title><![CDATA[No Man's Sky (PS4)]]></title><titleRefNo>46927</titleRefNo><groupTitle>No Mans Sky</groupTitle></game><ResultsInfo><Message></Message><SearchString></SearchString><SearchPlatform></SearchPlatform><Page>1</Page><PageSize>1</PageSize><MoreResultsAvailable>1</MoreResultsAvailable><RowCount>48490</RowCount></ResultsInfo></games>"

Solution

  • A couple of months later it turns out that this was simple. From the original code, I have changed the line:

    $dvdfeed = simplexml_load_file($dvdURL);
    

    to:

    $dvds= GetPage($dvdfeed );
    $dvdfeed = @simplexml_load_string($dvds);
    

    where GetPage() is a PHP function as follows:

    function GetPage ($URL, $PostParams = ''){
    // Set params...
    $ch = curl_init($URL);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 100);
    if ($PostParams <> ''){
        curl_setopt($ch, CURLOPT_POSTFIELDS, $PostParams);
    };
    $Page = curl_exec($ch);
    curl_close($ch);
    return $Page;
    };
    

    This now loads and parses the XML correctly, regardless of the encoding.