2010-08-14 143 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);