【发布时间】:2011-02-18 17:56:59
【问题描述】:
我正在开发一个CMS,它从用户的 Facebook URL(即http://facebook.com/users_unique_url)获取用户的个人资料图片。我怎样才能做到这一点?是否存在无需用户允许应用程序即可获取用户个人资料图片 URL 的 Faceboook API 调用?
【问题讨论】:
我正在开发一个CMS,它从用户的 Facebook URL(即http://facebook.com/users_unique_url)获取用户的个人资料图片。我怎样才能做到这一点?是否存在无需用户允许应用程序即可获取用户个人资料图片 URL 的 Faceboook API 调用?
【问题讨论】:
只需通过这个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。
【讨论】:
?type=large 查询字符串。提出一个比我正在输入的屏幕抓取更好的答案的道具,顺便说一句:)。
<img src="//graph.facebook.com/sarfraz.anees/picture" />...该协议被浏览器检测到,您不会在 HTTPS 上收到混合内容警告。
有办法做到这一点;)
感谢“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);
}
}
}
【讨论】:
一种方法是在他的回答中使用代码Gamlet posted:
另存为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;
我想我会发布这个,因为我花了一些时间来弄清楚细节......即使个人资料图片是公开的,当你 @987654322 时你仍然需要有一个访问令牌才能获得它@它。
【讨论】:
博文 Grab the Picture of a Facebook Graph Object 可能会提供另一种形式的解决方案。将教程中的代码与 Facebook 的 Graph API 及其 PHP SDK 库一起使用。
...并且尽量不要使用 file_get_contents(除非您准备好面对后果 - 请参阅 file_get_contents vs curl)。
【讨论】:
显示:
<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。
【讨论】:
要获取图像 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,而不是 USERNAME。您可以在那里获取您的 facebook id:
【讨论】:
更新:
从 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 可以是用户标识号或用户名。
要获取特定尺寸的用户个人资料图片,请调用
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/图片,您将获得方形类型。
澄清
如果你打电话(按照上面的例子)
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 数据而不是重定向到图像
我在想 - 也许 ID 会是一个有用的工具。每次用户创建一个新帐户时,它都应该获得一个更高的 ID。我用谷歌搜索,发现有一种方法可以通过 ID 估计帐户创建日期,并且来自 metadatascience.com 的 Massoud Seifi 收集了一些很好的数据。
阅读这篇文章:
这里有一些 ID 可供下载:
【讨论】:
@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);
}
这将返回 raw Facebook 头像图像 URL。那就随心所欲地去做吧!
【讨论】:
在您的服务器上保存完整尺寸的个人资料图像的简单的一行代码。
<?php
copy("https://graph.facebook.com/FACEBOOKID/picture?width=9999&height=9999", "picture.jpg");
?>
这只有在 php.ini 中启用 openssl 时才有效。
【讨论】:
将此作为评论添加到已接受的答案中,但认为它值得更长的解释。从 2015 年 4 月左右开始,这可能会提高几次。
从图 api 的 V2 开始,接受的答案不再使用用户名。所以现在你首先需要用户 ID,你不能再使用用户名来获取它。更复杂的是,出于隐私原因,Facebook 现在正在更改每个应用程序的用户 ID(请参阅 https://developers.facebook.com/docs/graph-api/reference/v2.2/user/ 和 https://developers.facebook.com/docs/apps/upgrading/#upgrading_v2_0_user_ids ),因此您必须进行某种适当的身份验证才能检索您可以使用的用户 ID。从技术上讲,个人资料图片仍然是公开的,可以在 /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(甚至是替代用户 ID)来检索个人资料图片,请分享!与此同时,旧的图表网址在 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'];
}
您可以更改参数中的“类型”:
正方形:
最大宽度和高度为 50 像素。
小
最大宽度为 50 像素,最大高度为 150 像素。
正常
最大宽度为 100 像素,最大高度为 300 像素。
大
最大宽度为 200 像素,最大高度为 600 像素。
【讨论】:
您是否担心个人资料图片的大小?在使用 PHP 使用 Facebook 实现登录时。我们将向您展示在 Facebook PHP SDK 中获取大尺寸个人资料图片的简单方法。此外,您还可以获取 Facebook 个人资料的自定义尺寸图片。
使用以下代码行设置头像尺寸。
$userProfile = $facebook->api('/me?fields=picture.width(400).height(400)');
查看这个帖子:http://www.codexworld.com/how-to-guides/get-large-size-profile-picture-in-facebook-php-sdk/
【讨论】:
URI = https://graph.facebook.com/{}/picture?width=500'.format(uid)
您可以通过online facebook id finder tool获取配置文件URI
您还可以传递类型参数,可能值为 small、normal、large、square。
【讨论】: