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