2013-02-09 211 views
0

嗨,大家好我是新手在PHP程序和Facebook應用程序,我發現這個代碼,它適用於TEXT帖子,但我的要求是,我也必須上傳一張圖片,我試着通過addin ['source' =>'$ image_source'],但不起作用... 如何上傳照片?facebook-php-sdk如何將照片上傳到牆上或相冊?

<?php 
include_once("config.php"); 


if($_POST) 
{ 
    //Post variables we received from user 
    $userPageId  = $_POST["userpages"]; 
    $userMessage = $_POST["message"]; 
    $image_source = "http://www.example.com/image.jpg" 
    if(strlen($userMessage)<1) 
    { 
     //message is empty 
     $userMessage = 'No message was entered!'; 
    } 

     //HTTP POST request to PAGE_ID/feed with the publish_stream 
     $post_url = '/'.$userPageId.'/feed'; 

     //posts message on page statues 
     $msg_body = array(
     'message' => $userMessage, 
     'source' => '$image_source' 
     ); 

    if ($fbuser) { 
     try { 
      $postResult = $facebook->api($post_url, 'post', $msg_body); 
     } catch (FacebookApiException $e) { 
     echo $e->getMessage(); 
     } 
    }else{ 
    $loginUrl = $facebook->getLoginUrl(array('redirect_uri'=>$homeurl,'scope'=>$fbPermissions)); 
    header('Location: ' . $loginUrl); 
    } 

    //Show sucess message 
    if($postResult) 
    { 
     echo '<html><head><title>Message Posted</title><link href="style.css" rel="stylesheet" type="text/css" /></head><body>'; 
     echo '<div id="fbpageform" class="pageform" align="center">'; 
     echo '<h1>Your message is posted on your facebook wall.</h1>'; 
     echo '<a class="button" href="'.$homeurl.'">Back to Main Page</a> <a target="_blank" class="button" href="http://www.facebook.com/'.$userPageId.'">Visit Your Page</a>'; 
     echo '</div>'; 
     echo '</body></html>'; 
    } 
} 

?> 

回答

3

您不能使用feed端點發布照片。您應該使用/me/photos以及publish_stream權限。要發佈到相冊您需要的/ALBUM_ID/photos端點

$msg_body = array(
    'message' => $userMessage, 
    'source' => '@'.'$image_source' 
    ); 

    $postResult = $facebook->api('/me/photos/','post', $msg_body); 

對於使用不與網站相關的網址,你需要使用url

$msg_body = array(
    'message' => $userMessage, 
    'url' => 'http://somepage.com/img.png' 
    ); 
+0

謝謝你們的重放,但我發現thiss消息 致命錯誤:Uncaught CurlException:26:無法創建formpost數據丟失/homez.666/makestre/www/autopost/inc/base_facebook.php在線972 – 2013-02-09 16:08:50

+0

@ user1898399在最新的PHP SDK 972行是一個括號.. 。https://github.com/facebook/facebook-php-sdk/blob/master/src/base_facebook.php#L972所以我不確定你是什麼發誓。 – phwd 2013-02-09 16:11:45

+0

我改變了庫,但我仍然有這條消息,但在977行,它是這樣的代碼: if($ result === false){}} {0129} => curl_errno($ CH), '錯誤'=>數組( '消息'=> curl_error($ CH), '類型'=> 'CurlException', ) )); curl_close($ ch); 扔$ e; } curl_close($ ch); 返回$結果; } – 2013-02-09 16:19:12