如何通过Facebook提供的图片url保存Facebook个人资料图片


How to save Facebook profile image by image url provided by Facebook?

我使用以下代码通过facebbok 提供的url保存了facebook个人资料图片

$data = file_get_contents('https://graph.facebook.com/[App-Scoped-ID]/picture?width=378&height=378&access_token=[Access-Token]');
$file = fopen('fblogo/fbphoto.jpg', 'w+');
fputs($file, $data);
fclose($file);

图像保存在所需位置,但保存了0字节意味着损坏,请告知如何正确保存图像

这看起来像是我从这个线程得到的答案:使用cURL 保存facebook个人资料图像

尝试在API调用中添加redirect=false以获取图片的真实位置。如果这不起作用,请尝试CURL:

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/' . $app_scoped_id . '/picture?width=378&height=378&redirect=false&access_token=xxx');
$data = curl_exec($ch);
$data = json_decode($data);

这将获得真实的URL,您可以使用另一个调用来获取图像:

curl_setopt($ch, CURLOPT_URL, $data->data->url);
$data = curl_exec($ch);
curl_close($ch);

如果这也不起作用,请确保您的提供商支持CURL请求:)

我使用这个:

$image = json_decode(file_get_contents("https://graph.facebook.com/$data[id]/picture?width=800&height=600&redirect=false"),true);
$image = file_get_contents($image['data']['url']);
file_put_contents("desired_filename.jpg",$image);