0

I am Trying to Tag a picture and posting it through graphs However, when I remove the 'tags' => $tags from below, it works. Otherwise I get this error:

Array ( [error] => Array ( [message] => (#100) param tags must be an array. [type] => OAuthException [code] => 100 ) )

Here is my code:

<?php
$tags = array(
    'to' => $_SESSION['my_fb_id'],
    'x' => 0,
    'y' => 0
);

 $tag[]= $tags ;
//
//upload photo
$file = 'imgtmp/save_as_this_name.jpg';
$args = array(
    'message' => 'This is my Picture',
    'tags' => $tag, // IF this line is removed ,It works!
);
$args[basename($file)] = '@' . realpath($file);
$ch = curl_init();
$url = 'https://graph.facebook.com/me/photos?access_token=' . $_SESSION['access_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);
print_r(json_decode($data, true));
?>
Yahoo
  • 4,093
  • 17
  • 59
  • 85

2 Answers2

2

Tags must be array of tags as you can tag many people.

$tag1 = array(
    'tag_text' => 'tag test1',
    'tag_uid' => 'XXXXX1',
    'x' => 0,
    'y' => 0
);

$tag2 = array(
    'tag_text' => 'tag test2',
    'tag_uid' => 'XXXXX2',
    'x' => 0,
    'y' => 0
);

$tags = array($tag1, $tag2);

In your case

$args = array(
    'message' => 'This is my Picture',
    'tags' => array( $tags ) , 
);

EDIT 1:

To tag photos successfully you will require user_photos permission.

Using graph api

$file = 'test.jpg';
$tags = array(
    'tag_text' => 'tag test',
    'tag_uid' => 'XXXXX',
    'x' => 10,
    'y' => 10
);

$args['tags'] = array($tags);
$args[basename($file)] = '@' . realpath($file);
$data = $facebook->api("/me/photos", "post", $args);

print_r($data);

Edit 2:

Just use json_encode for tags parameter

$args['tags'] = json_encode(array($tags));

This will solve the issue while using cURL.

Abhishek
  • 838
  • 1
  • 6
  • 9
  • the problem is `user_photos` permission, you require it to tag photos. I have tested it with using this permission and it worked. – Abhishek Sep 09 '12 at 06:19
  • I have the `user_photos` permission too ... but I am not using the `$data = $facebook->api("/me/photos", "post", $args);` I am doing this using `Curl` . Is this creating the problem ? how could you edit the code for using`curl` – Yahoo Sep 09 '12 at 07:03
  • also above you have mentioned `to` and below `Tag_uid` ? – Yahoo Sep 09 '12 at 07:09
  • Can you help me with a similar problem ? http://stackoverflow.com/questions/12500223/ – Yahoo Sep 19 '12 at 18:00
0

Alright I got it. From the Facebook API:

"tags": {
            "data": [
               {
                  "id": "11111111111111",
                  "name": "John Doe",
                  "x": 0,
                  "y": 0,
                  "created_time": "2012-09-03T03:08:44+0000"
               }
            ]
         },

The tags arg needs to contain an array with data as the key. Here:

$tags['data'] = array( 'to' => $_SESSION['my_fb_id'], 'x' => 0, 'y' => 0 );

christopher
  • 615
  • 5
  • 11
  • I'll just try that . Question: If I want to tag to people then ? `$tags = array([ 'to' => $_SESSION['my_fb_id'], 'x' => 0, 'y' => 0`],[ 'to' => $_SESSION['my_fb_id2'], 'x' => 0, 'y' => 0`]); – Yahoo Sep 09 '12 at 04:56
  • Yes , It exists . I can post the image if I remove the ` 'tags' => $tags, ` Line – Yahoo Sep 09 '12 at 05:08
  • = It still didn't work with this `$tags['data'] = array( 'to' => $_SESSION['my_fb_id'], 'x' => 0, 'y' => 0 );` – Yahoo Sep 09 '12 at 05:31