如何使用php在Facebook中保存用户的个人资料图片 [英] how to save user's profile pic in facebook using php
问题描述
函数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); //这是我保存
fwrite($ savefile,$ sourcecode)的新文件的名称;
fclose($ savefile);
这里$ uid是用户的id。
以上代码无法正常工作。
但是当我复制$ userpicpath(即 http://graph.facebook.com/ $ uid / picture?type = normal)在浏览器中,然后按Enter键将返回到我的地址栏中的图像的新路径,并显示正确的我想要的形象如果我将地址栏中的新路径传递给我的功能,则会保存我想要的图像文件。
为什么会发生这种情况?我如何获得第二个图像路径,并将其传递给我的程序中的功能。请帮助我。
谢谢。
Facebook使用重定向允许在网站上轻松嵌入。它通过发送HTTP 302重定向头来实现这一点。由于我看到你正在使用CURL,所以我已经根据我在网上找到的指南写了我的例子。我也张贴了如何通过cURL发送用户代理。这是我的getFBResponse()函数,可以用来替换$ userpicpath的赋值。尝试这样:$ userpicpath = getFBRedirect();
//只调用头
curl_setopt($ ch,CURLOPT_HEADER,true); // header将在输出
curl_setopt($ ch,CURLOPT_CUSTOMREQUEST,'HEAD'); // HTTP请求是'HEAD'
$ content = curl_exec($ ch);
//响应应为:
/ *
HTTP / 1.1 302找到
位置:http://www.iana.org/domains/example/
* /
//所以在Location:上分割应该给出一个索引数组2,你想要的URL是第二个索引(1)
$ theUrl = split($ content, 位置: );
return $ content [1];
}
Hi I m trying to create app that uses user's profile pic in it. So I write code that reads profile pic from facebook and save it on my server. I use following code
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);
Here $uid is id of user.
The above code doesn't work properly.
But when I copy the $userpicpath(ie http://graph.facebook.com/$uid/picture?type=normal) in browser and press enter it will return me new path to image in address bar and shows me proper image that I want. If i passed this new path in address bar to my function it saves image file that I want.
Why this is happening? How i get that second image path and pass it to my function in program. Please Help me.
Thanks.
Facebook uses a redirect to allow for easy embedding on websites. It accomplishes this by sending a HTTP 302 redirect header. Since I see you are using a CURL, I have written my example based on a guide I have found online. I also posted how to send the user agent via cURL. Here's my getFBResponse() function for you, and can be used to replace $userpicpath's assignment. Try this: $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在Facebook中保存用户的个人资料图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!