나는 CMS 페이스 북에서 페이스 북의 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 확장 기능이 해당 URL을 읽으려면 PHP의 file_get_contents
기능을 사용하도록 설정되어 있는지 확인해야합니다.
표시 방법 :
<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}}">
그래프 API v2.0은 사용자 이름을 사용하여 쿼리 할 수 없으며 는항상 userId
을 사용해야합니다.
업데이트 :
2012 년 8 월말부터 다양한 크기의 사용자 프로필 사진을 검색 할 수 있도록 API가 업데이트되었습니다. 선택적 width 및 height 필드를 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
}
}
END UPDATE
사용자의 프로필 사진을 얻으려면 전화하십시오.
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/그림을 호출하면 정사각형 형식이됩니다.
설명
위의 예에 따라 전화를 걸면
https://graph.facebook.com/redbull/picture?width=140&height=110
JSON response Facebook SDK 요청 방법 중 하나를 사용하는 경우를 반환합니다. 그렇지 않으면 이미지 자체를 반환합니다. 항상 JSON을 검색하려면 다음을 추가하십시오.
&redirect=false
이렇게 :
https://graph.facebook.com/redbull/picture?width=140&height=110&redirect=false
바이너리가 아닌 이미지 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이 아닌 ID를 사용해야합니다. 페이스 북의 ID를 얻을 수 있습니다.
서버에 FULL 크기 프로파일 이미지를 저장하는 간단한 한 줄 코드.
<?php
copy("https://graph.facebook.com/FACEBOOKID/picture?width=9999&height=9999", "picture.jpg");
?>
이것은 openssl이 php.ini에서 활성화 된 경우에만 작동합니다.
답을 받아 들일 때 주석으로 추가했으나 더 이상 설명 할 자격이 없다고 느꼈습니다. 2015 년 4 월 경부터이 방법이 몇 번 제기 될 것입니다.
그래프 api의 V2에서 허용 된 대답은 더 이상 사용자 이름을 사용하여 작동하지 않습니다. 이제는 먼저 userid가 필요하며 더 이상 사용자 이름을 사용하여이를 얻을 수 없습니다. 문제를 더욱 복잡하게하기 위해 개인 정보 보호를 위해 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에서 볼 수 있습니다 (docs at https://developers.facebook.com/docs/graph-api/reference/v2.0/user 참조)./그림 및이 예제 사용자 : http://graph.facebook.com/v2.2/4/picture?redirect=0 ) 그러나 사용자의 표준 사용자 ID를 알아내는 것은 자신의 프로필만으로는 불가능한 것처럼 보입니다. 앱에서 내 유스 케이스의 앱과의 상호 작용을 승인하도록해야합니다 (FB 프로필 옆에 프로필 사진 표시). 링크)은 잔인합니다.
누군가가 사용자 이름을 기반으로 프로필 사진을 얻는 방법을 찾은 경우 또는 대안으로 프로필 사진을 검색하는 데 사용하기 위해 사용자 ID (대체 이름)를 얻는 방법을 공유하십시오. 그동안 기존 그래프 URL은 2015 년 4 월까지 계속 작동합니다.
한 가지 방법은 코드를 사용하는 것입니다 Gamlet이 게시했습니다 :
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;
세부 사항을 파악하는 데 약간의 시간이 필요했기 때문에 게시하겠다고 생각했습니다 ... 프로필 사진은 공개되었지만 여전히 액세스 토큰을 가져야 만 컬 그것.
2015 년 4 월 (PHP 5 SDK없이) Working PHP (HTTP GET) 솔루션 :
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'];
}
매개 변수에서 'type'을 변경할 수 있습니다 :
광장:
최대 너비와 높이가 50 픽셀입니다.
작은
최대 너비는 50 픽셀이고 최대 높이는 150 픽셀입니다.
표준
최대 너비는 100 픽셀이고 최대 높이는 300 픽셀입니다.
큰
최대 너비는 200 픽셀이고 최대 높이는 600 픽셀입니다.
그것을 할 수있는 방법이있다;)
감사합니다 " 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는 유용한 도구가 될 것이다. 사용자가 새 계정을 만들 때마다 더 높은 ID를 얻어야합니다. 나는 인터넷 검색을 통해 ID로 계정 생성 날짜를 추정하는 방법이 있으며 metadatascience.com의 Massoud Seifi가 그것에 대한 좋은 데이터를 모았 음을 알았습니다.
이 기사 읽기 :
다운로드 할 수있는 ID는 다음과 같습니다.
블로그 게시물 Facebook 그래프 객체 그림는 다른 형태의 솔루션을 제공 할 수 있습니다. 페이스 북의 Graph API 및 PHP SDK 라이브러리와 함께 튜토리얼의 코드를 사용하십시오.
... file_get_contents 를 사용하지 마십시오 (결과에 직면 할 준비가되지 않은 경우 - file_get_contents vs curl 참조).
프로필 사진의 크기가 걱정 되십니까? PHP를 사용하여 페이스 북으로 로그인을 구현할 때. 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/
@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이 반환됩니다. 그때 당신이 원하는대로 무엇이든 할 자유롭게하십시오!
URI = https://graph.facebook.com/{}/picture?width=500'.format(uid)
당신은 온라인 facebook id 찾기 도구 를 통해 프로필 URI를 얻을 수 있습니다.
가능한 값이 small, normal, large, square 인 형식 param을 전달할 수도 있습니다.
공식 문서 를 참조하십시오.