O'Reilly logo

Facebook Cookbook by Jay Goldman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Adding Tags to Photos

Problem

I need to add tags to photos that have been stored in the Facebook Photos app.

Solution

Use the Photos.addTag() method, which unfortunately isn’t supported in the PHP Client Library (see Adding Missing PHP Client Library Methods):

$result = $facebook->api_client->photos_addTag('1234567890123456789',
'12345',null,50,50,null,null);

The parameters, in order, are the pid (photo ID) of the target photo; the uid (user ID) of the user you’re tagging (set to null if this is a text tag); the text to tag (set to null if this is a user tag); the x coordinate of the tag; the y coordinate of the tag; a JSON-formatted array of tag information; and the uid of the owner of the photo (if it isn’t the current loggedinuser).

Discussion

Tags can only be added to pending photos owned by either the current loggedinuser or the user specified in the last argument, unless your app has been granted the photo_upload extended permission (see Extended Permissions for more information).

The parameters for Photos.addTag() are pretty self-explanatory, except for the optional tags JSON-formatted array. If you pass a tags array, the tag_id, tag_text, x, and y parameters are ignored. The tags string should be constructed of x and y values accompanied by either a tag_uid or tag_text:

[{"x":"30.0","y":"30.0","tag_uid":12345}, {"x":"70.0","y":"70.0","tag_text":"
Joe's skateboard"}]

If you’re building your app in PHP 5.2 or higher, you can create a regular PHP array and then use the built-in JSON ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required