我正在开发一个CMS,它可以从用户的Facebook URL(即http://facebook.com/users_unique_url)中获取用户的个人资料图像。我怎样才能做到呢?是否有一个facebook API调用,获取用户的个人资料图像URL,而不需要用户允许应用程序?
当前回答
我在想,也许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/
其他回答
有办法做到这一点;)
感谢“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);
}
}
}
更新:
从2012年8月底开始,API已经更新,允许您检索不同大小的用户头像。添加可选的宽度和高度字段作为URL参数:
https://graph.facebook.com/USER_ID/picture?width=WIDTH&height=HEIGHT
其中WIDTH和HEIGHT是您请求的维度值。
这将返回一个最小宽度x高度的头像,同时尝试保持纵横比。例如,
https://graph.facebook.com/redbull/picture?width=140&height=110
返回
{
"data": {
"url": "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash4/c0.19.180.142/s148x148/2624_134501175351_4831452_a.jpg",
"width": 148,
"height": 117,
"is_silhouette": false
}
}
最后更新
要获取用户的头像,请呼叫
https://graph.facebook.com/USER_ID/picture
其中USER_ID可以是用户id号或用户名。
要获取特定大小的用户头像,请调用
https://graph.facebook.com/USER_ID/picture?type=SIZE
哪里SIZE应该替换为其中一个单词
square
small
normal
large
这取决于你想要的尺寸。
这个调用将返回一个URL到单个图像,其大小基于您所选择的类型参数。
例如:
https://graph.facebook.com/USER_ID/picture?type=small
返回图像的一个小版本的URL。
API只指定配置文件图像的最大大小,而不是实际大小。
广场:
最大宽度和高度为50像素。
小
最大宽度为50像素,最大高度为150像素。
正常的
最大宽度为100像素,最大高度为300像素。
大
最大宽度为200像素,最大高度为600像素。
如果你调用默认的USER_ID/picture,你会得到方形类型。
澄清
如果你打电话(如上例所示)
https://graph.facebook.com/redbull/picture?width=140&height=110
如果你使用的是Facebook sdk的请求方法,它会返回一个JSON响应。否则它将返回图像本身。要始终检索JSON,添加:
&redirect=false
像这样:
https://graph.facebook.com/redbull/picture?width=140&height=110&redirect=false
通过这个URL简单地获取数据:
http://graph.facebook.com/userid_here/picture
将userid_here替换为您想获取照片的用户的id。你也可以使用HTTPS。
可以使用PHP的file_get_contents函数读取URL并处理检索到的数据。
资源:
http://developers.facebook.com/docs/api
注意:在PHP .ini中,您需要确保OpenSSL扩展已启用,以便使用PHP的file_get_contents函数读取该URL。
简单的一行代码,保存完整尺寸的档案图像在您的服务器上。
<?php
copy("https://graph.facebook.com/FACEBOOKID/picture?width=9999&height=9999", "picture.jpg");
?>
只有在php.ini中启用了openssl,这才有效。
@NaturalBornCamper,
好的代码!这里是一个简洁的代码函数这样的过程!
function get_raw_facebook_avatar_url($uid)
{
$array = get_headers('https://graph.facebook.com/'.$uid.'/picture?type=large', 1);
return (isset($array['Location']) ? $array['Location'] : FALSE);
}
这将返回原始的Facebook头像图像URL。那你想怎么处置就怎么处置吧!
推荐文章
- PHP中的三个点(…)是什么意思?
- Guzzlehttp -如何从guzzle6得到响应的正文?
- 移动一个文件到服务器上的另一个文件夹
- Laravel中使用雄辩的ORM进行批量插入
- PHP 5.4调用时引用传递-容易修复可用吗?
- 格式化字节到千字节,兆字节,千兆字节
- 如何在PHP中获得变量名作为字符串?
- 用“+”(数组联合运算符)合并两个数组如何工作?
- Laravel PHP命令未找到
- 如何修复从源代码安装PHP时未发现xml2-config的错误?
- 在PHP中对动态变量名使用大括号
- 如何从对象数组中通过对象属性找到条目?
- 如何从关联数组中删除键及其值?
- PHP字符串中的花括号
- PHP -如何最好地确定当前调用是否来自CLI或web服务器?