Reputation: 1181
file_get_contents in PHP/Apache2 was getting user pictures from Facebook. It was working fine until recently. Now, it always times out after a minute, with this error in my Apache2 error.log:
PHP Warning: file_get_contents(https://graph.facebook.com/999999999/picture?width=200): failed to open stream: Connection timed out
Here is the code (I recently added $context to see if it made it work. It did not):
$context = stream_context_create(array('https' => array('header'=>'Connection: close\r\n')));
$fbprofileimage = file_get_contents('https://graph.facebook.com/'.$id.'/picture?width=100',false,$context);
I tried curl and it doesn't work:
$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_URL,'http://graph.facebook.com/'.$id.'/picture?width=100');
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_USERAGENT, 'StockBet');
$fbprofileimage = curl_exec($curl_handle);
curl_close($curl_handle);
I found out that file_get_contents & curl will work with some sites, but not others.
The following works:
$context = stream_context_create(array('https' => array('header'=>'Connection: close\r\n')));
$fbprofileimage = file_get_contents('https://fm.cnbc.com/applications/cnbc.com/resources/img/editorial/2017/08/03/104629909-GettyImages-630953738-bitcoin.240x160.jpg?v=1501760634',false,$context);
$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_URL,'https://fm.cnbc.com/applications/cnbc.com/resources/img/editorial/2017/08/03/104629909-GettyImages-630953738-bitcoin.240x160.jpg?v=1501760634');
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_USERAGENT, 'StockBet');
$fbprofileimage = curl_exec($curl_handle);
curl_close($curl_handle);
The above code can get the following image files as well:
The above code cannot get the following image files:
Does anyone know why I can get image files from some sites but not others?
Upvotes: 0
Views: 902
Reputation: 1181
For my purposes, which is to get user pictures from Facebook, I got it to work by using the following code:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // need confirmed
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // need confirmed. I think this is key, as Facebook redirects to another URL and we need to follow
//curl_setopt($ch, CURLOPT_ENCODING,""); // not needed confirmed
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1); // need confirmed
curl_setopt($ch, CURLOPT_TIMEOUT,1); // need confirmed
curl_setopt($ch, CURLOPT_FAILONERROR,true); // not needed confirmed
//curl_setopt($ch, CURLOPT_VERBOSE, true); // not needed confirmed
//curl_setopt($ch, CURLINFO_HEADER_OUT, true); // not needed confirmed
curl_setopt($ch, CURLOPT_HEADER, true); // need confirmed
$fbprofileimage = curl_exec($ch);
if (curl_errno($ch)){
echo 'Retreive Base Page Error: ' . curl_error($ch);
}
else {
//$info = rawurldecode(var_export(curl_getinfo($ch),true));
// Get the cookies:
$skip = intval(curl_getinfo($ch, CURLINFO_HEADER_SIZE));
//$responseHeader= substr($fbprofileimage,0,$skip);
$fbprofileimage= substr($fbprofileimage,$skip); // need confirmed
//echo "HEADER: $responseHeader\n"; // causes error
//echo "\n\nINFO: $info\n\nDATA: $fbprofileimage"; // causes error
}
I think what was key was this:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
because when getting https://graph.facebook.com/999999999/picture?width=200 (where 999999999 is the user's ID), Facebook redirects to another URL.
Upvotes: 1