views:

261

answers:

1

Hey,

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

A: 

Photo id is unique for every user and looks like two numbers joined by underscore in the middle.

Getting this id is a bit tricky.

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

For example we have CocaCola user with userid 40796308305. To get photo ids 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 a test console on this page)

This would return our photo ids:

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

I didn't work with photos much, maybe you don't have to go through all this process to get photo id, it might be some simple algorithm like <OWNER_ID>_<PHOTO_ID>. But try to get your photo id from FQL and see if tagging would work. If it does maybe you will be able to skip FQL part and build photo id from existing data you have.

Hopefully this helps.

serg
I have tried with a sample photo, only with the test console. It worked when i did pid= 40796308305_2011591Basically I have no idea of FQL, how to call it?And what's the album id of a application-photo-album?Hope you can help me finish this :)Greetz,Camillo
Camillo
@Camillo You can read about FQL here: http://developers.facebook.com/docs/reference/fql/ and play with it in a test console from the link in the answer which would show you how your request url should look like and where to send it.
serg

related questions