简体   繁体   English

像页面照片的按钮

[英]Like button to a page photo

Can you tell me with steps how can I do a like button in my site (php) to like a page photo on facebook? 你能告诉我步骤如何在我的网站(php)中做一个类似按钮,在Facebook上喜欢页面照片?

I know that I have to use the GRAPH API and have to do the POST via HTTP to /likes .. but I dont know how can I do it with PHP code. 我知道我必须使用GRAPH API并且必须通过HTTP来执行POST到/喜欢..但我不知道如何使用PHP代码执行此操作。

Somebody have an example? 有人有一个例子吗?

Thank you 谢谢

As long as you have obtained the publish_stream permission from the user you can like any photo you need to. 只要您从用户那里获得了publish_stream权限,就可以获得所需的任何照片。 If you are attempting to like the photo as a page be sure you have an access_token for the page (obtained via the /accounts connection on the user account). 如果您尝试将照片视为页面,请确保您拥有该页面的access_token (通过用户帐户上的/accounts连接获得)。

Once you have the access token the like is as simple as issuing an HTTP POST to a URL that looks similar to this: 获得访问令牌之后,就像向类似于此的URL发出HTTP POST一样简单:

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

Photo_ID = Photo ID in Facebook Photo_ID = Facebook中的照片ID

Access_Token = Access token obtained from Facebook with the publish_stream permission. Access_Token =使用publish_stream权限从Facebook获取的访问令牌。

UPDATE UPDATE


PHP Sample Code based on PHP Form CURL Post 基于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);

I would check this though as I'm not sure how accurate it is since I don't normally code PHP. 我会检查这个,因为我不确定它是多么准确,因为我通常不编码PHP。 In any manner, the post should be a raw HTTP POST request. 无论如何,帖子应该是原始的HTTP POST请求。

Fabio here is the php post snippet that i was able to get working. Fabio这里是我能够工作的php帖子片段。 Snippet includes a curl to get application access token and the api post to like an object, in this case a post on my app. Snippet包括一个curl来获取应用程序访问令牌,api帖子包含一个对象,在这种情况下是我的应用程序上的一个帖子。

  • The Sample post is here: Shows the post to be liked 示例帖子在此处:显示要播放的帖子

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

Getting the Application Access Token. 获取应用程序访问令牌。

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

Looking in url for postid parameter then liking id 在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');
}

You can get permissions by building an array of perms for login url. 您可以通过为登录URL构建一组权限来获取权限。 below i am requesting read_stream,publish_stream,publish_actions,offline_access in the scope for permissions. 下面我在权限范围内请求read_stream,publish_stream,publish_actions,offline_access。

NOTE: app access token needed for logout url. 注意:注销网址需要应用访问令牌。

    <?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