通过此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。
展示如下:
<img src="//graph.facebook.com/{{fid}}/picture">
<img src="//graph.facebook.com/{{fid}}/picture?type=large">
注意: 不要使用此方法,请查看下面 @Foreever 的评论。
$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}}">
Graph API v2.0不能使用用户名进行查询,你应该始终使用userId
。
更新:
从2012年8月底开始,API已经更新,允许您检索用户的个人资料图片以不同的尺寸。将可选的宽度和高度字段添加为URL参数:
https://graph.facebook.com/USER_ID/picture?width=WIDTH&height=HEIGHT
其中WIDTH
和HEIGHT
是您请求的尺寸参数。
这将返回一个最小尺寸为WIDTH
x HEIGHT
的个人资料图片,并尝试保持其宽高比。例如,
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
&redirect=false
,应该加上以返回 JSON 数据而不是重定向到图片。 - andrewtweber$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:
一个简单的一行代码,可以将完整尺寸的个人资料图片保存在您的服务器上。
<?php
copy("https://graph.facebook.com/FACEBOOKID/picture?width=9999&height=9999", "picture.jpg");
?>
只有在php.ini启用了openssl才能使其工作。
添加了对已接受答案的评论,但觉得它值得进一步解释。自2015年4月左右开始,这可能会被提起多次。
自图形API V2推出后,使用用户名的已接受答案不再适用。现在你需要先获取userid,而你不能再使用用户名来获取它。为了进一步复杂化问题,出于隐私原因,Facebook正在更改每个应用程序的userid(请参见https://developers.facebook.com/docs/graph-api/reference/v2.2/user/和https://developers.facebook.com/docs/apps/upgrading/#upgrading_v2_0_user_ids),因此你将需要某种适当的身份验证才能检索到可用的userid。从技术上讲,个人资料图片仍然是公开的,并可在/ userid / picture中使用(请参见https://developers.facebook.com/docs/graph-api/reference/v2.0/user/picture和此示例用户:http://graph.facebook.com/v2.2/4/picture?redirect=0)。但是,仅凭他们的个人资料信息似乎无法找到用户的标准userid-你的应用程序需要让他们批准与应用程序的交互,而对于我的用例(仅在他们的FB个人资料链接旁边显示个人资料图片),这太过麻烦。
如果有人已经找到了根据用户名获取个人资料图片的方法,或者另外一种获取userid(即使是备用的)以用来检索个人资料图片的方法,请分享!同时,旧的图形URL仍然有效,直到2015年4月。
这是一个2015年4月的PHP(HTTP GET)工作解决方案(不需要PHP 5 SDK):
function get_facebook_user_avatar($fbId){
$json = file_get_contents('https://graph.facebook.com/v2.5/'.$fbId.'/picture?type=large&redirect=false');
$picture = json_decode($json, true);
return $picture['data']['url'];
}
有一种方法是使用Gamlet在他的回答中发布的代码:
Save it as curl.php
Then in your file:
require 'curl.php';
$photo="https://graph.facebook.com/me/picture?access_token=" . $session['access_token'];
$sample = new sfFacebookPhoto;
$thephotoURL = $sample->getRealUrl($photo);
echo $thephotoURL;
我想分享这个内容,因为我花了一些时间才弄清楚具体细节...即使个人资料图片是公开的,当你使用cURL时仍需要在其中加入访问令牌才能获取它。
有一种方法可以做到这一点 ;)
感谢 "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);
}
}
}
阅读本文:
这里是一些可供下载的ID:
?type=large
查询字符串。顺便说一下,您提供的答案比我正在输入的屏幕抓取要好多了 :). - Domenic<img src="//graph.facebook.com/sarfraz.anees/picture" />
。浏览器会检测协议,因此在HTTPS上你不会收到混合内容警告。请注意,翻译后的内容应该与原始内容保持一致,且不需要解释或提供额外的信息。 - Chris Jacob