我正在使用CMS从他们的Facebook URL(即http://facebook.com/users_unique_url)获取用户的个人资料图像.我怎么能做到这一点?是否有一个Faceboook API调用,用于获取用户的配置文件图像URL,而无需用户需要允许该应用程序?
Sar*_*raz 408
只需通过以下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扩展以使用file_get_contentsPHP 的功能来读取该URL.
nei*_*ker 267
显示:
<img src="//graph.facebook.com/{{fid}}/picture">
Run Code Online (Sandbox Code Playgroud)
<img src="//graph.facebook.com/{{fid}}/picture?type=large">
Run Code Online (Sandbox Code Playgroud)
注意:不要使用它.请参阅下面的@ Foreever评论.
$img = file_get_contents('https://graph.facebook.com/'.$fid.'/picture?type=large');
$file = dirname(__file__).'/avatar/'.$fid.'.jpg';
file_put_contents($file, $img);
Run Code Online (Sandbox Code Playgroud)
其中$ fid是您在Facebook上的用户ID.
注意:如果图像标记为"18+",您将需要来自18岁以上用户的有效access_token:
<img src="//graph.facebook.com/{{fid}}/picture?access_token={{access_token}}">
Run Code Online (Sandbox Code Playgroud)
无法使用用户名查询Graph API v2.0,您应该userId始终使用.
Gun*_*son 163
更新:
从2012年8月底开始,API已更新,允许您检索不同大小的用户个人资料照片.将可选的宽度和高度字段添加为URL参数:
https://graph.facebook.com/USER_ID/picture?width=WIDTH&height=HEIGHT
Run Code Online (Sandbox Code Playgroud)
在哪里WIDTH和HEIGHT是您要求的维度值.
这将返回最小尺寸为WIDTHx 的轮廓图片,HEIGHT同时尝试保留纵横比.例如,
https://graph.facebook.com/redbull/picture?width=140&height=110
Run Code Online (Sandbox Code Playgroud)
回报
{
"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
}
}
Run Code Online (Sandbox Code Playgroud)
结束更新
要获取用户的个人资料照片,请致电
https://graph.facebook.com/USER_ID/picture
Run Code Online (Sandbox Code Playgroud)
其中USER_ID可以是用户标识号或用户名.
要获取特定大小的用户个人资料照片,请致电
https://graph.facebook.com/USER_ID/picture?type=SIZE
Run Code Online (Sandbox Code Playgroud)
其中SIZE应用一个关键词来代替
square
small
normal
large
Run Code Online (Sandbox Code Playgroud)
取决于你想要的大小.
此调用将返回一个URL到单个图像,其大小基于您选择的类型参数.
例如:
https://graph.facebook.com/USER_ID/picture?type=small
Run Code Online (Sandbox Code Playgroud)
返回一个小版本图像的URL.
API仅指定配置文件图像的最大大小,而不是实际大小.
广场:
最大宽度和高度为50像素.
小
最大宽度为50像素,最大高度为150像素.
正常
最大宽度为100像素,最大高度为300像素.
大
最大宽度为200像素,最大高度为600像素.
如果您调用默认的USER_ID /图片,则会获得方形类型.
澄清
如果你打电话(如上例所示)
https://graph.facebook.com/redbull/picture?width=140&height=110
Run Code Online (Sandbox Code Playgroud)
如果您正在使用其中一个Facebook SDK请求方法,它将返回JSON响应.否则它将返回图像本身.要始终检索JSON,请添加:
&redirect=false
Run Code Online (Sandbox Code Playgroud)
像这样:
https://graph.facebook.com/redbull/picture?width=140&height=110&redirect=false
Run Code Online (Sandbox Code Playgroud)
Nat*_*per 20
要获取图像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";
Run Code Online (Sandbox Code Playgroud)
您必须使用您的FACEBOOK ID,而不是USERNAME.你可以在那里获得你的Facebook ID:
Arm*_*and 19
简单的单行代码,用于在服务器上保存完整大小的配置文件图像.
<?php
copy("https://graph.facebook.com/FACEBOOKID/picture?width=9999&height=9999", "picture.jpg");
?>
Run Code Online (Sandbox Code Playgroud)
这只有在php.ini中启用openssl时才有效.
sck*_*ckd 10
添加此作为对已接受答案的评论,但认为它应该得到更长的解释.从2015年4月左右开始,这可能会提高几次.
从图表api的V2开始,接受的答案不再使用用户名.所以现在你首先需要userid,你不能再使用用户名来获取它.为了进一步复杂化,出于隐私原因,Facebook现在正在更改用户ID的每个应用程序(请参阅https://developers.facebook.com/docs/graph-api/reference/v2.2/user/和https://developers.facebook .com/docs/apps/upgracing/#ilevation_v2_0_user_ids),因此您必须进行某种正确的身份验证才能检索您可以使用的用户ID.从技术上讲,配置文件pic仍然是公开的,可从/ userid/picture获得(参见https://developers.facebook.com/docs/graph-api/reference/v2.0/user/picture上的文档和此示例用户:http: //graph.facebook.com/v2.2/4/picture?redirect=0)然而,基于他们的个人资料,找出用户的标准用户ID似乎是不可能的 - 你的应用需要让他们批准与应用的互动我的用例(仅显示他们的FB个人资料链接旁边的个人资料图片)是过度的.
如果有人想出了一种方法来获取基于用户名的个人资料图片,或者如何获得用户ID(甚至是交替的)用于检索个人资料图片,请分享!与此同时,旧的图表网址仍然有效,直到2015年4月.
保存为 curl.php
然后在你的文件中:
require 'curl.php';
$photo="https://graph.facebook.com/me/picture?access_token=" . $session['access_token'];
$sample = new sfFacebookPhoto;
$thephotoURL = $sample->getRealUrl($photo);
echo $thephotoURL;
Run Code Online (Sandbox Code Playgroud)我想我会发布这个,因为我花了一些时间来弄清楚细节......即使个人资料图片是公开的,你仍然需要在那里有一个访问令牌,以便在卷曲时获取它.
小智 8
有办法做到这一点;)
感谢" 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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
小智 8
从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'];
}
Run Code Online (Sandbox Code Playgroud)
您可以在参数中更改'type':
广场:
最大宽度和高度为50像素.
小
最大宽度为50像素,最大高度为150像素.
正常
最大宽度为100像素,最大高度为300像素.
大
最大宽度为200像素,最大高度为600像素.
我在想 - 也许ID会是一个有用的工具.每次用户创建新帐户时,都应获得更高的ID.我用谷歌搜索,发现有一种方法可以通过ID估算帐户创建日期,而来自metadatascience.com的Massoud Seifi收集了一些关于它的好数据.

阅读这篇文章:
以下是一些要下载的ID:
小智 6
博客文章抓取Facebook图形对象的图片可能会提供另一种形式的解决方案.使用教程中的代码以及Facebook的Graph API及其PHP SDK库.
...并尽量不使用file_get_contents(除非你准备好面对后果 - 请参阅file_get_contents vs curl).
| 归档时间: |
|
| 查看次数: |
434822 次 |
| 最近记录: |