繁体   English   中英

像页面照片的按钮

[英]Like button to a page photo

你能告诉我步骤如何在我的网站(php)中做一个类似按钮,在Facebook上喜欢页面照片?

我知道我必须使用GRAPH API并且必须通过HTTP来执行POST到/喜欢..但我不知道如何使用PHP代码执行此操作。

有人有一个例子吗?

谢谢

只要您从用户那里获得了publish_stream权限,就可以获得所需的任何照片。 如果您尝试将照片视为页面,请确保您拥有该页面的access_token (通过用户帐户上的/accounts连接获得)。

获得访问令牌之后,就像向类似于此的URL发出HTTP POST一样简单:

https://graph.facebook.com/PHOTO_ID/likes?access_token=ACCESS_TOKEN

Photo_ID = Facebook中的照片ID

Access_Token =使用publish_stream权限从Facebook获取的访问令牌。

UPDATE


基于PHP Form CURL Post的 PHP示例代码

<?php

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://graph.facebook.com/PHOTO_ID/likes");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);

$data = array(
    'Access_Token' => 'token_value'
);

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);

我会检查这个,因为我不确定它是多么准确,因为我通常不编码PHP。 无论如何,帖子应该是原始的HTTP POST请求。

Fabio这里是我能够工作的php帖子片段。 Snippet包括一个curl来获取应用程序访问令牌,api帖子包含一个对象,在这种情况下是我的应用程序上的一个帖子。

  • 示例帖子在此处:显示要播放的帖子

https://shawnsspace.com/plugins/TimeLinePost.php?pageid=135669679827333&postid=135669679827333_151602784936066&type=feed&fh=750

获取应用程序访问令牌。

function GetCH(){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://graph.facebook.com/oauth/access_token?client_id=YOUR_APP_ID&client_secret=YOUR_APP_SECRET&grant_type=client_credentials");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT_MS,20000);
if(substr($url,0,8)=='https://'){
    // The following ensures SSL always works. A little detail:
    // SSL does two things at once:
    //  1. it encrypts communication
    //  2. it ensures the target party is who it claims to be.
    // In short, if the following code is allowed, CURL won't check if the 
    // certificate is known and valid, however, it still encrypts communication.
    curl_setopt($ch,CURLOPT_HTTPAUTH,CURLAUTH_ANY);
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
}
$sendCH = curl_exec($ch);
curl_close($ch);
return $sendCH;
};
$app_access_token = GetCH();   

在url中查找postid参数然后喜欢id

  if($_GET['postid']){
  $postid = $_GET['postid'];
  }else{
  $postid = '135669679827333_151602784936066';
  }

    if($user){
 $pageLike = $facebook->api('/'.$postid.'/likes?access_token='.$access_token.'&method=post', 'POST');
}

您可以通过为登录URL构建一组权限来获取权限。 下面我在权限范围内请求read_stream,publish_stream,publish_actions,offline_access。

注意:注销网址需要应用访问令牌。

    <?php
    $url = (!empty($_SERVER['HTTPS'])) ? 'https://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
        require './src/facebook.php';
    $facebook = new Facebook(array(
      'appId'  => 'APPID',
      'secret' => 'APP-SECRET',
      'cookie' => true, // enable optional cookie support
    ));
    $user = $facebook->getUser();
    if ($user) {
      try {
        // Proceed knowing you have a logged in user who's authenticated.
        $user_profile = $facebook->api('/me');

            //$pageInfo = $facebook->api('/'.$pageid.'?access_token='.$_SESSION['fb_112104298812138_access_token].');
            //$pageInfoUser = $user_profile[id];
      } catch (FacebookApiException $e) {
        error_log($e);
        $user = null;
      }
    }
    /*  */
    if ($user) {
      $logoutUrl = $facebook->getLogoutUrl();
    } else {
    $params = array(
      scope => 'read_stream,publish_stream,publish_actions,offline_access',
      redirect_uri => $url
    );
      $loginUrl = $facebook->getLoginUrl($params);
    }
    $access_token = $_SESSION['fb_135669679827333_access_token'];

    ?>
<?php
        if(!$user){
    echo ' : <a href="'.$loginUrl.'" target="_self">Login</a>  ';
    }else{
        echo '<a href="'.$logoutUrl.'?'.$app_access_token.'" target="_blank">Logout</a>';
        }
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM