odd
odd

Reputation: 449

Premature end of script headers: php-cgi -- While running a CURL script

before i go back to the Host (yet again) the only error log they are giving on the below script is:

Premature end of script headers: php-cgi

the scrip i am running which works on other servers and my local machine is but on this perticualr server is giving an error 500:

$ch = curl_init("http://feeds.energydigger.com/headlines.xml");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
$doc = new SimpleXmlElement($data, LIBXML_NOCDATA);

if(isset($doc->channel))
{
    parseRSS($doc);
}
function parseRSS($xml)
{
    $cnt = 3;
    for($i=0; $i<$cnt; $i++)
    {
        $url    = $xml->channel->item[$i]->link;
        $title  = $xml->channel->item[$i]->title;
        $desc = $xml->channel->item[$i]->description;
        $date = $xml->channel->item[$i]->pubDate;

        echo '<p><a href="'.$url.'">'.$title.'</a><br />'.$date.'</p>';
    }
}

Does anyone know what may generate the error, I can't say I have seen this one before... i am still trying to get hold of the PHP logs too.

Upvotes: 1

Views: 1370

Answers (1)

pilsetnieks
pilsetnieks

Reputation: 10420

You can get a 500 error, if an exception is thrown and not caught, and in PHP settings display_errors = 0. Most likely it is thrown by SimpleXML. Try wrapping the portion with XML operations in a try .. catch block and see what the exception is. For example:

$ch = curl_init("http://feeds.energydigger.com/headlines.xml");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);

try
{
    $doc = new SimpleXmlElement($data, LIBXML_NOCDATA);

    if(isset($doc->channel))
    {
        parseRSS($doc);
    }
    function parseRSS($xml)
    {
        $cnt = 3;
        for($i=0; $i<$cnt; $i++)
        {
            $url    = $xml->channel->item[$i]->link;
            $title  = $xml->channel->item[$i]->title;
            $desc = $xml->channel->item[$i]->description;
            $date = $xml->channel->item[$i]->pubDate;

            echo '<p><a href="'.$url.'">'.$title.'</a><br />'.$date.'</p>';
        }
    }
}
catch (Exception $e)
{
    echo $e -> getMessage();
}

Just in case, here's more about PHP exceptions.

Upvotes: 1

Related Questions