简体   繁体   中英

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?

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.

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. 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).

Once you have the access token the like is as simple as issuing an HTTP POST to a URL that looks similar to this:

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

Photo_ID = Photo ID in Facebook

Access_Token = Access token obtained from Facebook with the publish_stream permission.

UPDATE


PHP Sample Code based on PHP Form CURL Post

<?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. In any manner, the post should be a raw HTTP POST request.

Fabio here is the php post snippet that i was able to get working. 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.

  • 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

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

  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. below i am requesting read_stream,publish_stream,publish_actions,offline_access in the scope for permissions.

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>';
        }
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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