2012-08-14 30 views
0

我想知道當我用此代碼調出圖片時,我如何標記隨機朋友 此代碼打開相冊並上傳圖片。如何標記隨機好友上傳圖片

如果這裏有人能幫助我改變什麼,並添加代碼「

謝謝

<?php 
    require 'facebook.php'; 
    $facebook = new Facebook(array(
     'appId' => '', 
     'secret' => '', 
     'cookie' => true, 
    )); 

    $user = $facebook->getUser(); 
    if ($user) { 
     $facebook->setFileUploadSupport(true); 

     //Create an album 
     $album_details = array(
      'message'=> 'Album desc', 
      'name'=> 'Album name' 
     ); 
     $create_album = $facebook->api('/me/albums', 'post', $album_details); 

     //Get album ID of the album you've just created 
     $album_uid = $create_album['id']; 

     //Upload a photo to album of ID... 
     $photo_details = array(
      'message'=> 'Photo message' 
     ); 
     $file='images/logo.jpg'; //Example image file 
     $photo_details['image'] = '@' . realpath($file); 

     $upload_photo = $facebook->api('/'.$album_uid.'/photos', 'post', $photo_details); 
    } 

?> 
+0

爲什麼要標記」random 「照片上的朋友?僅供參考,Facebook不喜歡它,如果人們被標記在非真實「照片」的照片中,而是合成圖像或完全不同的東西,甚至可能不是真正的人。他們在文檔中的某處說,如果這種行爲經常發生,您的應用可能會被用來標記照片中的人物。 – CBroe 2012-08-14 13:23:16

+0

是的,我知道,但仍然, 希望它自動標記 – 2012-08-14 13:26:10

+1

那麼,請閱讀如何通過API標記在文檔中工作請。如果你有特定問題,請回到這裏。 – CBroe 2012-08-14 13:37:03

回答

0

根據documentation我添加了代碼編寫腳本

<?php 
define('COUNT_FRIENDS', '10'); 

require 'facebook.php'; 
$facebook = new Facebook(array(
    'appId' => '', 
    'secret' => '', 
    'cookie' => true, 
)); 

$user = $facebook->getUser(); 
if ($user) { 
    $facebook->setFileUploadSupport(true); 

    //Create an album 
    $album_details = array(
     'message'=> 'Album desc', 
     'name'=> 'Album name' 
    ); 
    $create_album = $facebook->api('/me/albums', 'post', $album_details); 

    //Get album ID of the album you've just created 
    $album_uid = $create_album['id']; 

    //Upload a photo to album of ID... 
    $photo_details = array(
     'message'=> 'Photo message' 
    ); 
    $file='images/logo.jpg'; //Example image file 
    $photo_details['image'] = '@' . realpath($file); 

    $upload_photo = $facebook->api('/'.$album_uid.'/photos', 'post', $photo_details); 

    /* 
    * Make tags on photo 
    */ 
    $photo_id = $upload_photo['id']; 

    /* 
    * Get friends info from FB 
    */ 
    $result = $fb_app->api('/me/friends'); 

    $friend_uids = array(); 
    if($result && $result['data']){ 
     foreach($result['data'] as $friend){ 
      $friend_uids[] = $friend['id']; 
     } 
    } 

    /* 
    * Choose random friends UID's 
    */ 
    $rand_keys = array_rand($friend_uids, COUNT_FRIENDS); 
    foreach($rand_keys as $key){ 
     $friends[] = $friend_uids[$key]; 
    } 

    foreach($friends as $friend_uid){ 
     $tag_params = array(
      'to'  => $friend_uid, 
      'tag_text' => 'Sample tag text', 
      'x'  => 0, 
      'y'  => 0 
     ); 
     $result = $facebook->api('/' . $photo_id . '/tags', 'POST', $tag_params); 
    } 
} 
?> 

只需要實現的功能」 getRandomFriends()「返回UID列表

+0

如何正確實現該功能? 謝謝 – 2012-08-14 18:34:03

+0

我添加了代碼來選擇隨機的朋友。插入常量'COUNT_FRIENDS'你的價值。 – user1587955 2012-08-15 15:06:21