gpt4 book ai didi

php - 如何使用 php 在 facebook 中保存用户的个人资料图片

转载 作者:可可西里 更新时间:2023-11-01 00:51:56 25 4
gpt4 key购买 nike

您好,我正在尝试创建在其中使用用户个人资料图片的应用程序。所以我编写代码从 facebook 读取个人资料图片并将其保存在我的服务器上。我使用以下代码

function GetImageFromUrl($link){
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch,CURLOPT_URL,$link);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result=curl_exec($ch);
curl_close($ch);
return $result;
}

$userpicpath = "http://graph.facebook.com/$uid/picture?type=normal";

$sourcecode = GetImageFromUrl($userpicpath);
$savefile = fopen("$uid-normal.jpg", "w"); //this is name of new file that i save
fwrite($savefile, $sourcecode);
fclose($savefile);

这里的$uid是用户的id。

上面的代码不能正常工作。

但是当我在浏览器中复制 $userpicpath(ie http://graph.facebook.com/ $uid/picture?type=normal) 并按回车键时,它会在地址栏中返回新的图像路径,并显示我想要的正确图像。如果我将地址栏中的这个新路径传递给我的函数,它会保存我想要的图像文件。

为什么会这样?我如何获得第二个图像路径并将其传递给我的程序中的函数。请帮助我。

谢谢。

最佳答案

Facebook 使用重定向来轻松嵌入网站。它通过发送 HTTP 302 重定向 header 来完成此操作。由于我看到您正在使用 CURL,因此我根据在网上找到的指南编写了我的示例。我还发布了如何通过 cURL 发送用户代理。这是我为您提供的 getFBResponse() 函数,可用于替换 $userpicpath 的赋值。试试这个:$userpicpath = getFBRedirect();

// Only calling the head
curl_setopt($ch, CURLOPT_HEADER, true); // header will be at output
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'HEAD'); // HTTP request is 'HEAD'

$content = curl_exec ($ch);

// The response should be:
/*
HTTP/1.1 302 Found
Location: http://www.iana.org/domains/example/
*/
// So splitting on "Location: " should give an array of index 2, the URL you want being the second index (1)
$theUrl = split($content, "Location: ");
return $content[1];

关于php - 如何使用 php 在 facebook 中保存用户的个人资料图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5429964/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com