How to tag photos in facebook-api?

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

I managed to create an album and upload a photo to it, but I stuck with the tags.

I have rights and the correct session key.

My code so far:

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 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 for a long time, if you know something that can help me, please write here: Thanks for your help, Camillo

+4
source share
3 answers

The photo ID is unique to each user and looks like two numbers connected by an underscore in the middle.

Getting this identifier is a bit complicated.

You can get it by running FQL on photo , but you need to provide an album ID, which is also unique to the user. You can get the album id from album , but you need to specify the owner userid.

For example, we have a CocaCola user with userid 40796308305. To get photo identifiers from this user, we can run FQL:

 SELECT pid FROM photo WHERE aid IN ( SELECT aid FROM album WHERE owner="40796308305") 

(you can run it in the test console on this page )

This will return our photo IDs:

 [ { "pid": "40796308305_2298049" }, { "pid": "40796308305_1504673" }, { "pid": "40796308305_2011591" }, ... ] 

I didnโ€™t work very much with photos, maybe you donโ€™t have to go through this whole process to get the photo identifier, it can be a simple algorithm, for example, <OWNER_ID>_<PHOTO_ID> . But try getting your photo id from FQL and see if tagging will work. If possible, you can skip the FQL part and create a photo identifier from existing data.

Hope this helps.

+3
source

Hey, you can tag the image directly in Upload using the GRAPH API, see the example below: This method creates an array for tag information, in this example 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 invoking the GRAPH API to load the image:

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

And here is the GRAPH API call method:

  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 $ albId argument contains the identifier from the Facebook album.

And if you want to tag an existing image from an album, you can use this method: First we need the correct image identifier from the REST API. In this example, we need the name from the album created by the application, or the user using this application. The method returns the identifier of the image from the last loaded image 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; } } 

You now have the correct image id. From the REST API, you can make your REST API CALL to tag this image. $ pid is the image from the getRestPhotoId method and $ tag_uid is 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 use CUROPT_GET instead of CUROPT_POST to add the tag to the REST API.

Hope this helps you.

Regards Kay from Stuttar

+9
source

I found out that the problem is that when you use curl to send an array, the post function does not send the array correctly, it only sends an "Array", so the graph API complained about tags that are an array. To solve this, I did the following:

 $data = array(array('tag_uid' => $taguser, 'x' => rand() % 100, 'y' => rand() % 100 )); $data = json_encode($data); $photo_details = array( 'message'=> $fnames, 'tags' => $data ); 

Now I just send using curl

 curl_setopt($ch, CURLOPT_POSTFIELDS, $photo_details); 
0
source

All Articles