2017-09-14 7 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); 

上記のコードは、同様に次の画像ファイルを取得することができます。

上記のコードは次の画像ファイルを取得することはできません。

私はいくつかのサイトではなく、他の人から画像ファイルを取得することができ、なぜ誰もが知っていますか?

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

のFacebookからのユーザーの写真を取得することである私の目的のために、

+0

CURLOPT_VERBOSEを使用してカール要求を記録しようとしましたか? curl_closeの前にcurl_getinfoを使用して、HTTPエラーが報告されているかどうかを確認することもできます。 ほとんどの場合、これらのサイトは、ユーザーエージェントまたはリファラーヘッダーを省略した場合、出力をブロックしている可能性があります。 –

+0

@RubenVincentenあなたのお返事ありがとうございます。私はそれを私の目的のために働かせました。私の答えは以下の通りです。 – Curt

答えて

1

は、私はそれが次のコードを使用して動作するようになりましたhttps://graph.facebook.com/999999999/picture?width=200(999999999はユーザーID)を取得すると、Facebookは別のURLにリダイレクトされるためです。

+0

ありがとう!それは確かにでした curl_setopt($ ch、CURLOPT_FOLLOWLOCATION、true); –

関連する問題