2013-05-16 55 views
1

我需要張貼在Facebook粉絲頁面的應用程序。 我的帖子類型是文本+圖片。 當我只發佈文本,我沒有問題,但是當我嘗試添加一個JPG它不起作用。Facebook的粉絲頁面上的帖子圖像與應用程序

這裏是我的代碼:

<pre> 
<?php 
require_once 'facebook.php'; 

// configuration 
$appid = 'XXX'; 
$appsecret = 'YYY'; 
$pageId = 'ZZZ'; 
$msg = 'MSG MSG MSG'; 
$title = 'TITOLO TITOLO TITOLO'; 
$uri = 'http://www.google.it/'; 
$desc = 'Description'; 
$pic = 'http://sharefavoritebibleverses.com/images/bible_verses.png'; 
$action_name = 'AZIONE NOME'; 
$action_link = 'http://www.yahoo.it'; 

$facebook = new Facebook(array(
    'appId' => $appid, 
    'secret' => $appsecret, 
    'cookie' => false, 
)); 

$user = $facebook->getUser(); 

// Contact Facebook and get token 
if ($user) 
{ 
// you're logged in, and we'll get user acces token for posting on the wall 
    try 
    { 
      $facebook->setFileUploadSupport(true); 
      $page_info = $facebook->api("/$pageId?fields=access_token"); 
      if (!empty($page_info['access_token'])) { 
      /*$attachment = array(
       'access_token' => $page_info['access_token'], 
       'message' => $msg, 
       'name' => $title, 
       'picture'=>$pic 
       );*/ 
      $photo_details = array(
       'message'  => 'message', 
       'access_token' => 'XYZ' 
       ); 
       $photo_details['picture'] = '@'.realpath('aaa.jpg'); 
       echo realpath('aaa.jpg'); 

       $status = $facebook->api("/$pageId/feed", "post", $photo_details); 
       } 
       else 
       { 
        $status = 'No access token recieved'; 
       } 
      } 
      catch (FacebookApiException $e) 
      { 
       error_log($e); 
       $user = null; 
      } 
} 
else 
{ 
    // you're not logged in, the application will try to log in to get a access token 
    header("Location:{$facebook->getLoginUrl(array('scope' => 'photo_upload,user_status,publish_stream,user_photos,manage_pages'))}"); 
} 

echo $status; 
echo "<br>CONT=".count($status); 
?> 

此外,我需要一個像第二個中的格式,而不是像第一:

enter image description here

什麼是錯在我的代碼?

謝謝!

回答

0

試試這個:

//At the time of writing it is necessary to enable upload support in the Facebook SDK, you do this with the line: 
$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='app.jpg'; //Example image file 
$photo_details['image'] = '@' . realpath($file); 

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

它適用於張貼在我的牆上,但我需要張貼在粉絲頁面(我是管理員)...我應該替換什麼? – aquanat

+0

好吧,剛剛在相冊創建中添加了'access_token'=> $ page_info ['access_token'],現在它按照我的意願工作。謝謝! – aquanat

0

請確保$ pic是服務器上的相對圖像位置,而不是圖像的通用網址。

+0

圖片我張貼的代碼是:$ photo_details [ '圖片'] = '@' 真實路徑( 'aaa.jpg');它位於應用程序index.php的相同文件夾中。我沒有使用$ pic ='http://sharefavoritebibleverses.com/images/bible_verses.png'; (以前用於嘗試使用外部jpg) – aquanat

+0

如果我確實echo realpath('aaa.jpg');我得到/var/www/html/fb/aaa.jpg – aquanat