2010-06-15 91 views
7

我已經得到了照片上傳功能,使用此代碼工作,Facebook的圖形API的照片上傳到粉絲專頁相冊

<?php 

include_once 'facebook-php-sdk/src/facebook.php'; 
include_once 'config.php';//this file contains the secret key and app id etc... 

$facebook = new Facebook(array(
    'appId' => FACEBOOK_APP_ID, 
    'secret' => FACEBOOK_SECRET_KEY, 
    'cookie' => true, 
    'domain' => 'your callback url goes here' 
)); 

$session = $facebook->getSession(); 

if (!$session) { 

    $url = $facebook->getLoginUrl(array(
       'canvas' => 1, 
       'fbconnect' => 0, 
       'req_perms'=>'user_photos,publish_stream,offline_access'//here I am requesting the required permissions, it should work with publish_stream alone, but I added the others just to be safe 
      )); 

    echo 'You are not logged in, please <a href="' . $facebook->getLoginUrl() . '">Login</a> to access this application'; 

} else{ 

    try { 

     $uid = $facebook->getUser(); 
     $me = $facebook->api('/me'); 
     $token = $session['access_token'];//here I get the token from the $session array 
     $album_id = 'the id of the album you wish to upload to eg: 1122'; 

     //upload your photo 
     $file= 'test.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)); 

    } catch(FacebookApiException $e){ 
     echo "Error:" . print_r($e, true); 
    } 
} 
?> 

我希望這可以幫助,我和一個朋友砸我們的頭靠在牆上相當有一段時間才能得到這個工作!

無論如何,這裏是我的問題,我怎樣才能將圖片上傳到粉絲頁面?我很努力地做到這一點,當我上傳圖像時,我所得到的只是照片的ID,但沒有照片。

所以基本上,當用戶點擊我們應用程序上的上傳按鈕時,我需要它將他們創建的圖片上傳到我們的粉絲頁面的相冊中,並將它們標記在相冊上。

任何人都知道我可以做到這一點?

回答

2

這似乎是一個圖形API的錯誤。看到這個http://forum.developers.facebook.com/viewtopic.php?id=59063(不能發佈鏈接,還)

+0

啊該死的...任何其他方式嘗試和鏈接到粉絲頁面的照片?有沒有辦法在上傳照片時對其進行標記? – Odyss3us 2010-06-18 10:37:37

+0

目前無法基於頁面進行身份驗證。但FB在週二發佈了一個修復方案,可以解決這個問題。我希望修復也會創建上傳照片到頁面的權限。 http://bugs.developers.facebook.com/show_bug.cgi?id=10005(具體請參閱評論#85和#98) – gsharma 2010-06-18 22:26:11

+0

讓我們希望他們做到正確! Thanx的答覆。 – Odyss3us 2010-06-22 07:06:07

4

按照簡單的獲取登錄URL與

$數據[ 'URL'] = $這個 - > facebook-> getLoginUrl(陣列(」範圍 '=>' publish_stream,read_stream,user_likes,user_photos,manage_pages'));

這將讓你張貼照片,並在fanpage中添加專輯。 獲得訪問令牌

$accounts = $this->facebook->api('/'.$PAGE_ID.'?fields=access_token', 'get', array('access_token' => $access_token)); 
$page_access_token = $accounts['access_token']; 

與其他細節創建相冊

  $album_details = array(
       'message'=> 'Test album', 
       'name'=> $today ,//should be unique each time, 
      'access_token'=>$page_access_token 
      ); 
      $album_created = $this->facebook->api('/'.$PAGE_ID.'/albums', 'post', $album_details); 

上傳圖片到particualar的fanpage專輯

$photo = $this->facebook->api('/'.$album_id.'/photos', 'POST', array(
             'source' => "@"."@".realpath(BASEPATH."../".$photopath), 
             'message' => 'new photo', 
         'access_token' => $page_access_token, 
         'no_story' => 0 
             ) 
            ); 

       //print_r($album_created['id']); 

,並更改路徑,如你所願 快樂編碼

這段代碼在這個網站中工作得很好....爲什麼downvoted.?如果你是該頁面的所有者,你可以發佈照片到fanpage。我有該應用程序的工作副本。

+1

可能是因爲他們不希望所有的用戶都成爲該頁面的所有者;) – dwery 2013-06-21 11:03:13

+0

親愛的,您好,請給我一份工作副本,用於創建相冊並上傳多張照片。需要幫助。謝謝。 – 2016-01-25 07:01:06

相關問題