2017-09-14 100 views
0

PHP/Apache2中的file_get_contents正在从Facebook获取用户图片。直到最近它一直工作正常。现在,它总是超时一分钟后,这个错误在我的Apache2 error.log中:PHP file_get_contents&curl可以从一些网站获取图像文件,但不能从其他网站获取图像文件。为什么?

PHP的警告:的file_get_contents(https://graph.facebook.com/999999999/picture?width=200):未能打开流:连接超时

这里代码(我最近增加了$背景下,看它是否使工作它没有。):

$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); 

我试着卷曲它不工作:

$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); 

我发现file_get_contents & curl可以与一些网站一起使用,但不能与其他网站一起使用。

以下工作:

$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); 

上面的代码可以得到下面的图像文件,以及:

上述代码无法得到以下图像文件:

有谁知道为什么我可以从一些网站而不是其他人得到的图像文件?

+0

您是否尝试过使用CURLOPT_VERBOSE记录curl请求?您还可以在curl_close之前使用curl_getinfo来查看是否报告了任何HTTP错误。 如果您省略用户代理或引用标头,那么这些网站很可能会阻止输出。 –

+0

@RubenVincenten感谢您的回复。我为它达成目的。看到我下面发布的答案。 – Curt

回答

1

对于我而言,这是从Facebook获取用户的照片,我把它通过使用下面的代码工作:

 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 
     }    

我认为是关键的是:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 

因为当获得https://graph.facebook.com/999999999/picture?width=200(其中999999999是用户的ID)时,Facebook将重定向到另一个URL。

+0

谢谢!这确实是 curl_setopt($ ch,CURLOPT_FOLLOWLOCATION,true); –