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

Get Facebook Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.