Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tag photos in facebook-api?

Tags:

facebook

I wanted to ask if/how is it possible to tag a photo using the FB API (Graph or REST).

I've managed to create an album and also to upload a photo in it, but I stuck on tagging.

I've got the permissions and the correct session key.

My code until now:

try {
    $uid = $facebook->getUser();
    $me = $facebook->api('/me');
    $token = $session['access_token'];//here I get the token from the $session array
    $album_id = $album[0];

    //upload photo
    $file= 'images/hand.jpg';
    $args = array(
        'message' => 'Photo from application',
    );
    $args[basename($file)] = '@' . realpath($file);

    $ch = curl_init();
    $url = 'https://graph.facebook.com/'.$album_id.'/photos?access_token='.$token;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
    $data = curl_exec($ch);

    //returns the id of the photo you just uploaded
    print_r(json_decode($data,true));

    $search = array('{"id":', "}");
    $delete = array("", "");

    // picture id call with $picture
    $picture = str_replace($search, $delete, $data);

    //here should be the photos.addTag, but i don't know how to solve this
    //above code works, below i don't know what is the error / what's missing

    $json = 'https://api.facebook.com/method/photos.addTag?pid='.urlencode($picture).'&tag_text=Test&x=50&y=50&access_token='.urlencode($token);

    $ch = curl_init();
    $url = $json;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_exec($ch);
} catch(FacebookApiException $e){
    echo "Error:" . print_r($e, true);
}

I really searched a long time, if you know something that might help me, please post it here :) Thanks for all your help, Camillo

like image 351
Camillo Avatar asked Dec 16 '22 22:12

Camillo


1 Answers

Hey, you can tag the Picture directly on the Upload with the GRAPH API, see the example below: This Method creates an array for the tag information, in this examples the method becomes an array with Facebook user Ids:

private function makeTagArray($userId) {
    foreach($userId as $id) {
          $tags[] = array('tag_uid'=>$id, 'x'=>$x,'y'=>$y);
          $x+=10;
          $y+=10;
      }
    $tags = json_encode($tags);
    return $tags;
}

Here are the arguments for the call of the GRAPH API to upload an picture:

$arguments = array(
                    'message' => 'The Comment on this Picture',
                    'tags'=>$this->makeTagArray($this->getRandomFriends($userId)),
                    'source' => '@' .realpath( BASEPATH . '/tmp/'.$imageName),
            );

And here is the Method for the GRAPH API call:

    public function uploadPhoto($albId,$arguments) {
    //https://graph.facebook.com/me/photos
    try {

       $fbUpload = $this->facebook->api('/'.$albId.'/photos?access_token='.$this->facebook->getAccessToken(),'post', $arguments);
       return $fbUpload;
    }catch(FacebookApiException $e) {
        $e;
       // var_dump($e);
        return false;
    }
}

The argument $albId contains an ID from an Facebook Album.

And if you want to Tag an existing Picture from an Album you can user this Method: At First we need the correct picture ID from the REST API, In this example we need the Name from an Album wich the Application has create or the user wich uses this Application. The Method returns The Picture ID From the last Uploaded Picture of this Album:

public function getRestPhotoId($userId,$albumName) {
     try {
        $arguments = array('method'=>'photos.getAlbums',
                            'uid'=>$userId
            );
       $fbLikes = $this->facebook->api($arguments);
       foreach($fbLikes as $album) {

           if($album['name'] == $albumName) {
               $myAlbId = $album['aid'];
           }
       }
       if(!isset($myAlbId))
           return FALSE;
       $arguments = array('method'=>'photos.get',
                            'aid'=>$myAlbId
            );
       $fbLikes = $this->facebook->api($arguments);
       $anz = count($fbLikes);
       var_dump($anz,$fbLikes[$anz-1]['pid']);
       if(isset($fbLikes[$anz-1]['pid']))
           return $fbLikes[$anz-1]['pid'];
       else
           return FALSE;
       //var_dump($fbLikes[$anz-1]['pid']);
       //return $fbLikes;
    }catch(FacebookApiException $e) {
        $e;
       // var_dump($e);
        return false;
    }
}

Now you have the correct picture ID From the REST API and you can make your REST API CALL to tag this Picture $pid is the Picture from the Method getRestPhotoId and $tag_uid is an Facebook userId:

    $json = 'https://api.facebook.com/method/photos.addTag?pid='.$pid.'&tag_uid='.$userId.'&x=50&y=50&access_token='.$this->facebook->getAccessToken();

    $ch = curl_init();
    $url = $json;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    //curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_GET, true);
    $data = curl_exec($ch);

And this line is very important: curl_setopt($ch, CURLOPT_GET, true); you must youse CUROPT_GET instead of CUROPT_POST to add a Tag throw the REST API.

I Hope this helps you.

Best wishes Kay from Stuttart

like image 122
Kay Schneider Avatar answered Jan 27 '23 19:01

Kay Schneider