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);
上記のコードは、同様に次の画像ファイルを取得することができます。
- https://b.thumbs.redditmedia.com/SSGv_d2P6ymZVIq8Bm5IVqpcz6WZioCfbRh5rxMPcTc.jpgを(Redditに)
- https://try.alexa.com/wp-content/uploads/2017/01/alexa-logo.png
- https://ir.ebaystatic.com/rs/v/fxxj3ttftm5ltcqnto1o4baovyl.png
(イーベイ) - https://images-na.ssl-images-amazon.com/images/G/01/Gateway/Unrec/T2/Amazon_GW_DesktopShoveler_5_200x200.CB503347722.png
- https://bitcointalk.org/useravatars/avatar_7110.jpg https://try.alexa.com/wp-content/uploads/2017/01/alexa-logo.png
上記のコードは次の画像ファイルを取得することはできません。
- https://www.google.ca/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png
- https://s.yimg.com/uu/api/res/1.2/7rUl71NcpOwqZeJexHuZhA--/Zmk9c3RyaW07aD0xOTM7cHlvZmY9MDtxPTk1O3c9MjIwO3NtPTE7YXBwaWQ9eXRhY2h5b24-/http://media.zenfs.com/en-US/homerun/aol_com_127/ce17fc9999cd894bf3a3dac1416b3230(ヤフー)
- https://c.disquscdn.com/next/a0cd712/marketing/assets/img/brand/disqus-logo-blue.svg(ディスカス)
- https://pbs.twimg.com/profile_images/464794058229964800/uwlkErTI_bigger.png(ツイッター)
- https://scontent-yyz1-1.xx.fbcdn.net/v/t45.1600-4/c0.31.284.149/p284x149/21337242_6083114387812_5783480223513182208_n.png?oh=3c78c007377ee96b844e91843dfdfddb&oe=5A183896 (Facebookの)
- https://www.ibm.com/cloud-computing/images/ca-en-watson-2-09112017-600x260-p1v1.jpg
私はいくつかのサイトではなく、他の人から画像ファイルを取得することができ、なぜ誰もが知っていますか?
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からのユーザーの写真を取得することである私の目的のために、
CURLOPT_VERBOSEを使用してカール要求を記録しようとしましたか? curl_closeの前にcurl_getinfoを使用して、HTTPエラーが報告されているかどうかを確認することもできます。 ほとんどの場合、これらのサイトは、ユーザーエージェントまたはリファラーヘッダーを省略した場合、出力をブロックしている可能性があります。 –
@RubenVincentenあなたのお返事ありがとうございます。私はそれを私の目的のために働かせました。私の答えは以下の通りです。 – Curt