2010-08-14 73 views
4

我想问一下使用FB API(Graph或REST)标记照片是否可以/如何。如何在facebook-api中标记照片?

我已经设法创建了一个相册,同时也上传了一张照片,但我坚持标记。

我有权限和正确的会话密钥。

我的代码至今:

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

我真的搜查了很长一段时间,如果你知道的东西,可能会帮助我,请在这里发表吧:) 感谢您的帮助, 卡米洛

+0

是你能够让它通过测试控制台的工作?它返回什么错误? http://developers.facebook.com/docs/reference/rest/photos.addTag – serg 2010-08-14 17:40:52

+0

它返回“121无效的照片ID” 我已经搜索了一整天只为了这一点,但似乎该方法已经消失/破碎/弃用... 如果有人知道解决方案,将不胜感激。 谢谢, 卡米洛 – Camillo 2010-08-14 17:49:34

+0

它不会在所有的工作,无论是从浏览器直接调用,也不由试验台,也不由卷曲...... 我对此很伤心,我很喜欢这个主意整合?此为我的应用程序:( 顺便说一句谢谢你的回答, 卡米洛 – Camillo 2010-08-14 17:52:32

回答

3

照片ID对于每个用户都是独一无二的,看起来像两个数字通过中间的下划线连接起来。

获取此ID有点棘手。

您可以通过在photo表上运行FQL来获得它,但您需要提供也是用户唯一的专辑ID。您可以从album表中获取专辑ID,但您需要提供所有者用户标识。

例如我们与用户ID 40796308305.可口可乐用户在此用户可以运行FQL得到照片的身份:

SELECT pid FROM photo WHERE aid IN (SELECT aid FROM album WHERE owner="40796308305") 

(你可以在测试控制台上this page运行)

这会回到我们的照片IDS:

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

我没有照片的工作很多,也许你不必经历这一切的过程中得到照片的身份证件,这可能是一些简单的算法,如<OWNER_ID>_<PHOTO_ID>。但是尝试从FQL获取照片ID并查看标记是否可行。如果确实如此,您可以跳过FQL部分并从现有数据构建照片ID。

希望这会有所帮助。

+0

我试着用示例照片,只用测试控制台。它工作时,我做了pid = 40796308305_2011591 基本上我不知道FQL,如何调用它? 什么是应用程序相册的相册ID? 希望你能帮助我完成这个:) 格尔茨, 卡米洛 – Camillo 2010-08-15 08:31:42

+0

@Camillo你可以在这里阅读有关FQL:http://developers.facebook.com/docs/reference/fql/并在测试它玩控制台从答案中的链接,它会告诉你你的请求url应该是什么样子以及在哪里发送它。 – serg 2010-08-15 15:02:10

+1

此FQL不再工作 – Sourav 2011-06-10 03:14:56

9

嘿, 可以直接与所述图形API上传标签的画面, 见下面的例子: 该方法创建的标签信息的数组,在这个实施例中的方法变得与Facebook用户ID的数组:

private function makeTagArray($userId) { 
    foreach($userId as $id) { 
      $tags[] = array('tag_uid'=>$id, 'x'=>$x,'y'=>$y); 
      $x+=10; 
      $y+=10; 
     } 
    $tags = json_encode($tags); 
    return $tags; 
} 

下面是上传图片参数为图形API的调用:

$arguments = array(
        'message' => 'The Comment on this Picture', 
        'tags'=>$this->makeTagArray($this->getRandomFriends($userId)), 
        'source' => '@' .realpath(BASEPATH . '/tmp/'.$imageName), 
      ); 

这里是图形API调用方法:

public function uploadPhoto($albId,$arguments) { 
    //https://graph.facebook.com/me/photos 
    try { 

     $fbUpload = $this->facebook->api('/'.$albId.'/photos?access_token='.$this->facebook->getAccessToken(),'post', $arguments); 
     return $fbUpload; 
    }catch(FacebookApiException $e) { 
     $e; 
     // var_dump($e); 
     return false; 
    } 
} 

参数$ albId包含来自Facebook相册的ID。

如果您想为相册中现有的图片添加标签,您可以使用此方法: 首先,我们需要来自REST API的正确图片ID,在此示例中,我们需要“应用”创建或使用此应用程序的用户。 该方法返回的图片ID从这张专辑的最后上传的图片:

public function getRestPhotoId($userId,$albumName) { 
    try { 
     $arguments = array('method'=>'photos.getAlbums', 
          'uid'=>$userId 
      ); 
     $fbLikes = $this->facebook->api($arguments); 
     foreach($fbLikes as $album) { 

      if($album['name'] == $albumName) { 
       $myAlbId = $album['aid']; 
      } 
     } 
     if(!isset($myAlbId)) 
      return FALSE; 
     $arguments = array('method'=>'photos.get', 
          'aid'=>$myAlbId 
      ); 
     $fbLikes = $this->facebook->api($arguments); 
     $anz = count($fbLikes); 
     var_dump($anz,$fbLikes[$anz-1]['pid']); 
     if(isset($fbLikes[$anz-1]['pid'])) 
      return $fbLikes[$anz-1]['pid']; 
     else 
      return FALSE; 
     //var_dump($fbLikes[$anz-1]['pid']); 
     //return $fbLikes; 
    }catch(FacebookApiException $e) { 
     $e; 
     // var_dump($e); 
     return false; 
    } 
} 

现在你有正确的图片ID从REST API,你可以让你的REST API调用来标记这个图片$ pid是从方法getRestPhotoId图片和$ tag_uid是一个Facebook的用户名:

$json = 'https://api.facebook.com/method/photos.addTag?pid='.$pid.'&tag_uid='.$userId.'&x=50&y=50&access_token='.$this->facebook->getAccessToken(); 

    $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_setopt($ch, CURLOPT_GET, true); 
    $data = curl_exec($ch); 

而此行是非常重要的: curl_setopt($ CH,CURLOPT_GET,真正的); 你必须使用CUROPT_GET而不是CUROPT_POST来添加一个Tag来抛出REST API。

我希望这可以帮助你。

顺祝凯从Stuttart

+0

我收到**致命错误:未捕获的OAuthException:(#121)无效的照片ID **使用您的代码,你能帮忙吗? – Sourav 2011-06-08 13:07:50

0

我发现了问题,问题是,当你使用curl发送阵列后的功能将无法正常发送阵列,它仅发送“阵列”,它为什么图表API抱怨标签是一个数组。为了解决这个我做了以下内容:

$data = array(array('tag_uid' => $taguser, 
        'x' => rand() % 100, 
        'y' => rand() % 100 
       )); 

$data = json_encode($data); 

$photo_details = array(
'message'=> $fnames, 
'tags' => $data 
); 

现在,我只是给使用curl

curl_setopt($ch, CURLOPT_POSTFIELDS, $photo_details);