我想在我的评论中显示我的关注者的 Facebook 个人资料图片

发布于 2024-12-09 16:45:33 字数 5468 浏览 0 评论 0原文

为此,我使用了两种方法,但都不起作用。 对于方法1,我下载了三个文件:

  1. base_facebook.php
  2. facebook.php
  3. fb_ca_chain_bundle.crt

方法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>";

方法2

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 的,我自己创建的。 我使用了 Avatars 代码,如果有任何 WordPress 用户在我的网站上发表评论,该代码可以正常工作。显示他/她的图像,但不显示 Facebook 用户的图像。

我正在使用此代码作为 WordPress Avatars,运行良好:

$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>";

For that purpose I used two methods but neither is working.
For Method 1 I downloaded three files:

  1. base_facebook.php
  2. facebook.php
  3. fb_ca_chain_bundle.crt

Method 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>";

Method 2

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);
       }
   }
}

I saved the above class as 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>";

As you can see I want to display images of Facebook users in my comments.
As in Wordpress or Joomla when a Facebook user comments, their Facebook profile image shows with comments. My website is not using Wordpress or Joomla.
It's PHP based, I created it myself.
I used Avatars code which is working fine if any Wordpress user comments on my website. His/her image shows but not Facebook user's.

I am using this code for Wordpress Avatars which is working fine:

$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>";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文