[英]I want to show facebook profile images of my followers in my comments
為此,我使用了兩種方法,但都沒有用。 對於方法1,我下載了三個文件:
方法1
include ('facebook.php');
$facebook = new facebook('API','SECRET');
$fbid = $facebook->getUser();
echo $fbid; // when i echo this it always show 0; even the followers are logedin or logedout
echo "<div class='' id=\"user-icon\"><img src=\"'https://graph.facebook.com/{'.$fbid.'}/picture{?type=small}'\" /></div>";
方法二
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 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);
// curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, true);
//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);
curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, true);
$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);
}
}
}
我將上面的類保存為curl.php。
require_once 'curl.php';
$photo="https://graph.facebook.com/me/picture?access_token=".$session['access_token'];
$sample = new sfFacebookPhoto;
$thephotoURL=$sample->getRealUrl($photo);
echo $thephotoURL;
// but here echo is not showing anything no-error, no-exception, no-output.
echo "<div id=\"user-icon\"><img src=\"$thephotoURL\" /></div>";
如您所見,我想在評論中顯示Facebook用戶的圖像。 就像在Wordpress或Joomla中,當Facebook用戶發表評論時,他們的Facebook個人資料圖片會顯示評論。 我的網站未使用Wordpress或Joomla。 它基於PHP,是我自己創建的。 我使用了頭像代碼,如果我的網站上有任何Wordpress用戶評論,該代碼都可以正常工作。 他/她的圖像顯示了,但Facebook用戶沒有顯示。
我正在使用此代碼進行Wordpress化身,它工作正常:
$hash = md5( strtolower( trim( $row['email'] ) ) );
$default_usr = urlencode( 'http://www.kingofdevelopers.com/images/user-icon.png');
echo "<div class='default-user' id=\"user-icon\"><img src=\"http://www.gravatar.com/avatar /$hash.'.jpg?s=45&d=$default_usr'\" /></div>";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.