我正在开发一个CMS,它可以从用户的Facebook URL(即http://facebook.com/users_unique_url)中获取用户的个人资料图像。我怎样才能做到呢?是否有一个facebook API调用,获取用户的个人资料图像URL,而不需要用户允许应用程序?
当前回答
博客文章“抓取Facebook图形对象的图片”可能会提供另一种解决方案。使用教程中的代码以及Facebook的Graph API及其PHP SDK库。
... 并且尽量不要使用file_get_contents(除非您已经准备好面对后果——参见file_get_contents vs curl)。
其他回答
我在想,也许ID会是一个有用的工具。每当用户创建一个新帐户时,它应该获得一个更高的ID。我谷歌了一下,发现有一种方法可以通过ID来估计帐户创建日期,Massoud Seifi从metadatascience.com收集了一些关于它的好数据。
阅读这篇文章:
http://metadatascience.com/2013/03/11/inferring-facebook-account-creation-date-from-facebook-user-id/
下面是一些可以下载的id:
http://metadatascience.com/2013/03/14/lookup-table-for-inferring-facebook-account-creation-date-from-facebook-user-id/
获取图片URL,而不是二进制内容:
$url = "http://graph.facebook.com/$fbId/picture?type=$size";
$headers = get_headers($url, 1);
if( isset($headers['Location']) )
echo $headers['Location']; // string
else
echo "ERROR";
你必须使用你的FACEBOOK ID,而不是用户名。你可以在那里获得你的facebook id:
http://findmyfbid.com/
显示:
50×50像素
<img src="//graph.facebook.com/{{fid}}/picture">
200像素宽度
<img src="//graph.facebook.com/{{fid}}/picture?type=large">
保存(使用PHP)
注意:不要使用这个。请看下面@ forever的评论。
$img = file_get_contents('https://graph.facebook.com/'.$fid.'/picture?type=large');
$file = dirname(__file__).'/avatar/'.$fid.'.jpg';
file_put_contents($file, $img);
$fid是你在Facebook上的用户id。
注意:对于标记为“18+”的图像,您将需要18+用户的有效access_token:
<img src="//graph.facebook.com/{{fid}}/picture?access_token={{access_token}}">
2015年更新:
Graph API v2.0不能使用用户名查询,您应该始终使用userId。
有办法做到这一点;)
感谢“http://it.toolbox.com/wiki/index.php/Use_curl_from_PHP_-_processing_response_headers”:
<?php
/**
* Facebook user photo downloader
*/
class sfFacebookPhoto {
private $useragent = 'Loximi sfFacebookPhoto PHP5 (cURL)';
private $curl = null;
private $response_meta_info = array();
private $header = array(
"Accept-Encoding: gzip,deflate",
"Accept-Charset: utf-8;q=0.7,*;q=0.7",
"Connection: close"
);
public function __construct() {
$this->curl = curl_init();
register_shutdown_function(array($this, 'shutdown'));
}
/**
* Get the real URL for the picture to use after
*/
public function getRealUrl($photoLink) {
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $this->header);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, false);
curl_setopt($this->curl, CURLOPT_HEADER, false);
curl_setopt($this->curl, CURLOPT_USERAGENT, $this->useragent);
curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($this->curl, CURLOPT_TIMEOUT, 15);
curl_setopt($this->curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($this->curl, CURLOPT_URL, $photoLink);
//This assumes your code is into a class method, and
//uses $this->readHeader as the callback function.
curl_setopt($this->curl, CURLOPT_HEADERFUNCTION, array(&$this, 'readHeader'));
$response = curl_exec($this->curl);
if (!curl_errno($this->curl)) {
$info = curl_getinfo($this->curl);
var_dump($info);
if ($info["http_code"] == 302) {
$headers = $this->getHeaders();
if (isset($headers['fileUrl'])) {
return $headers['fileUrl'];
}
}
}
return false;
}
/**
* Download Facebook user photo
*
*/
public function download($fileName) {
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $this->header);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->curl, CURLOPT_HEADER, false);
curl_setopt($this->curl, CURLOPT_USERAGENT, $this->useragent);
curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($this->curl, CURLOPT_TIMEOUT, 15);
curl_setopt($this->curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($this->curl, CURLOPT_URL, $fileName);
$response = curl_exec($this->curl);
$return = false;
if (!curl_errno($this->curl)) {
$parts = explode('.', $fileName);
$ext = array_pop($parts);
$return = sfConfig::get('sf_upload_dir') . '/tmp/' . uniqid('fbphoto') . '.' . $ext;
file_put_contents($return, $response);
}
return $return;
}
/**
* cURL callback function for reading and processing headers.
* Override this for your needs.
*
* @param object $ch
* @param string $header
* @return integer
*/
private function readHeader($ch, $header) {
//Extracting example data: filename from header field Content-Disposition
$filename = $this->extractCustomHeader('Location: ', '\n', $header);
if ($filename) {
$this->response_meta_info['fileUrl'] = trim($filename);
}
return strlen($header);
}
private function extractCustomHeader($start, $end, $header) {
$pattern = '/'. $start .'(.*?)'. $end .'/';
if (preg_match($pattern, $header, $result)) {
return $result[1];
}
else {
return false;
}
}
public function getHeaders() {
return $this->response_meta_info;
}
/**
* Cleanup resources
*/
public function shutdown() {
if($this->curl) {
curl_close($this->curl);
}
}
}
将此作为已接受答案的评论,但觉得它值得更长的解释。从2015年4月开始,这可能会提高几次。
As of V2 of the graph api the accepted answer no longer works using a username. So now you need the userid first, and you can no longer use a username to get this. To further complicate matters, for privacy reasons, Facebook is now changing userid's per app (see https://developers.facebook.com/docs/graph-api/reference/v2.2/user/ and https://developers.facebook.com/docs/apps/upgrading/#upgrading_v2_0_user_ids ), so you will have to have some kind of proper authentication to retrieve a userid you can use. Technically the profile pic is still public and available at /userid/picture (see docs at https://developers.facebook.com/docs/graph-api/reference/v2.0/user/picture and this example user: http://graph.facebook.com/v2.2/4/picture?redirect=0) however figuring out a user's standard userid seems impossible based just on their profile - your app would need to get them to approve interaction with the app which for my use case (just showing a profile pic next to their FB profile link) is overkill.
如果有人想出了一种方法来获得基于用户名的个人资料图片,或者,如何获得一个用户id(甚至是一个交替的)来检索个人资料图片,请分享!与此同时,旧的图形url在2015年4月之前仍然有效。
推荐文章
- 编写器更新和安装之间有什么区别?
- 本地机器上的PHP服务器?
- 如何评论laravel .env文件?
- Android Facebook集成无效键散列
- 在PHP中检测移动设备的最简单方法
- 如何在树枝模板中呈现DateTime对象
- 如何创建Android Facebook密钥哈希?
- 如何删除查询字符串,只得到URL?
- 您是否可以“编译”PHP代码并上传一个二进制文件,该文件将由字节码解释器运行?
- 非法字符串偏移警告PHP
- 从数组中获取随机项
- 为什么一个函数检查字符串是否为空总是返回true?
- 如何使用Laravel迁移将时间戳列的默认值设置为当前时间戳?
- 如何增加php的最大执行时间
- 将给定日期与今天进行比较