Question: 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

Answer: 
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](http://www.electrictoolbox.com/php-curl-form-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.
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>';
        }
?>

```