Fun with parsing retrieved XML.

The search engine for the website that I administer and develop is provided by a third party: query is made against their cluster, XML formatted data comes back, is parsed and displayed.

It stopped working this morning.

I traced the problem down to a PHP Sablotron/XML parsing error (“xml declaration not at start of external entity”). Seems the server that’s connected to now sends response headers back to the client and these headers were getting in the way of Sablotron doing it’s job.

A simple one-liner fix got things working again, as shown in the middle of this sample code:


$fp = fsockopen($queryhost, 80);
if(
$fp) {
    
fputs($fp, $header);
    while(!
feof($fp)) {
        
$xml .= fgets($fp, 128);
    }
}
fclose($fp);

//ensure string starts with the XML declaration.
$xml = substr($xml, strpos($xml, "<?xml") );

$xsltproc = xslt_create();
$result = xslt_process($xsltproc,'arg:/_xml',$xsltfile,NULL,array('/_xml' => $xml));

Comments are closed.