Forum Moderators: coopster
I have a very serious problem that is causing my script to just send back a completely blank page with the HTTP/0.9 200 OK response header for no apparent reason. The code is as follows (simplified):
<?php// Display errors
ini_set('display_errors',1);
error_reporting(E_ALL¦E_STRICT);
// Set up options
$toSet[CURLOPT_CONNECTTIMEOUT] = 20; // Time to wait for connection
$toSet[CURLOPT_TIMEOUT] = 45; // Time to wait for whole operation
$toSet[CURLOPT_RETURNTRANSFER] = true; // Return transfer instead of printing
$toSet[CURLOPT_FAILONERROR] = true;
// Forward the custom headers
if ( isset($_SERVER['HTTP_USER_AGENT']) ) $toSet[CURLOPT_USERAGENT] = $_SERVER['HTTP_USER_AGENT'];
// Show SSL without verifying
$toSet[CURLOPT_SSL_VERIFYPEER] = false;
$toSet[CURLOPT_SSL_VERIFYHOST] = false;
// Start curl
$curl = curl_init('http://www.facebook.com/');
// Load options
curl_setopt_array($curl, $toSet);
// Fetch page
$return = curl_exec($curl);
// Check for curl errors
if ( $error = curl_error($curl) )
echo 'ERROR: ',$error;
// Close handle
curl_close($curl);
// Print
echo $return;
?>
I've tried tweaking lots of different cURL options, searching for hours (found an occasional similar problem but never a solution or even explanation) and came up with nothing so I'm pretty stumped here. It also seems to only affect Facebook - every other site has worked out fine. The other strange thing is it seems to only affect certain configurations - my local machine has no problem with it, nor did my old VPS (the provider of which has now run off, hence no longer using it) but my current hosting acts as described, i.e. waits about 12 seconds then shows me a blank page.
It gives up at some point during the curl_exec() call and I'd be tempted to point the blame at cURL and leave it as out of my hands but it gets even stranger. In the process of debugging the script I used the CURLOPT_VERBOSE option and logged everything to a file. Upon running the script the same thing happened but then about 10 seconds after the browser stops loading/waiting for the page, my log file was filled with text that showed everything had worked fine - I was even able to record the response into the text file.
I don't really know enough about HTTP but at a guess, for some reason the server is cutting off the connection with my browser before the script finishes? I've obviously tried increasing every timeout limit value I can find but I'm not sure that's the problem - PHP would normally give an error message. I've just got nothing to work with here.
If anyone has anything to offer it would very very much appreciated. Thanks!
<?php
//
// Display errors
ini_set('display_errors',1);
error_reporting(E_ALL¦E_STRICT);
//
// Set up options
$toSet[CURLOPT_CONNECTTIMEOUT] = 20; // Time to wait for connection
$toSet[CURLOPT_TIMEOUT] = 45; // Time to wait for whole operation
$toSet[CURLOPT_RETURNTRANSFER] = true; // Return transfer instead of printing
$toSet[CURLOPT_FAILONERROR] = true;
//
// Forward the custom headers
if ( isset($_SERVER['HTTP_USER_AGENT']) ) $toSet[CURLOPT_USERAGENT] = $_SERVER['HTTP_USER_AGENT'];
//
// Show SSL without verifying
$toSet[CURLOPT_SSL_VERIFYPEER] = false;
$toSet[CURLOPT_SSL_VERIFYHOST] = false;
//
// Start curl
$curl = curl_init('http://www.facebook.com/');
//
// Load options
curl_setopt_array($curl, $toSet);
//
// Fetch page
$return = curl_exec($curl);
// You will not necessarily get a string back, if the request failed for
// any reason, the return value will be the boolean false,
// you have to take special care for such a case. I'd suggest to examine
// the value of $return like this before going further in your code:
// if ($return!== false) echo $return; else echo curl_error($curl);
//
// Check for curl errors
if ( $error = curl_error($curl) )
echo 'ERROR: ',$error;
// curl_error does not return a logical value (see its documentation), but
// a string, which is empty when there was no error. Use the condition
// I described above to decide whether there is an error condition or not.
//
// Close handle
curl_close($curl);
//
echo $return;
// See my comment after curl_exec, you should make sure the $return does
// not contain a boolean false, because outputting that with echo will result
// in a null string sent to the browser.
?>
So I'd suggest you to add an extra check after the curl_exec, and make sure the return value is not false, if it's not, dump the response to the browser, if false, then use the curl_error to output the error.
This will probably not solve the problem, but at least will make your code better, and might produce some output from curl about the root cause.
After the curl_exec() I can't output anything, even an echo "curl_exec() complete"; won't display. I can do other stuff like write to a file with the contents of the return but that only completes about 10 seconds after the browser stops loading and displays the blank page.
By the way, what is the version of php and curl you are using?
...
$h = fopen('php://output','w');
fwrite($h, 'TEST123');
...
It would also be interesting to see what the server sends back to your browser. If I remember well this version 0.9 response is the sign of a broken (or empty) http response. If you have a packet (or a http traffic) monitor at hand use that one, otherwise I'll suggest you to download Ethereal [ethereal.com].