如何在facebook-api中标记照片? [英] How to tag photos in facebook-api?

查看:87
本文介绍了如何在facebook-api中标记照片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想询问是否/如何使用FB API(Graph或REST)标记照片。

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.

我的代码到现在为止:

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

我真的搜索了很长时间,如果你知道可能会帮助我的东西,请张贴在这里:)
感谢所有的帮助,
Camillo

I really searched a long time, if you know something that might help me, please post it here :) Thanks for all your help, Camillo

推荐答案

照片ID是唯一的对于每个用户,并且看起来像在中间加下划线的两个数字。

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

获取此ID有点棘手。

Getting this id is a bit tricky.

您可以通过在 照片 表,但您需要提供也是用户唯一的相册ID。您可以从 相册获取相册ID 表,但您需要提供所有者userid。

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.

例如,我们有CocaCola用户与用户标识40796308305.要从该用户获取照片,我们可以运行FQL:

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")

(您可以在此页面

这将返回我们的照片ID:

This would return our photo ids:

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

我没有使用照片,也许你不必经过所有这个过程获得照片ID,它可能是一些简单的算法,如< OWNER_ID> _< PHOTO_ID> 。但是尝试从FQL获取您的照片ID,看看标签是否可行。如果可能,您可以跳过FQL部分,并从现有数据中构建照片ID。

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.

希望这有助于。

这篇关于如何在facebook-api中标记照片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆