2015-02-08 68 views
0

我有一個使用Facebook PHP API的應用程序。這是我的代碼的一部分,我不知道我做錯了什麼,或者我現在錯過了什麼。我似乎無法在Facebook上發佈用戶時間線上的圖片。任何幫助將非常感激。非常感謝。在facebook上發佈圖片用戶的時間線

 <?php 
      echo "<p> Hello World!</p>"; 
     // php 5.3 and up can throw an error if this is not set 
     date_default_timezone_set("Europe/London"); 


     // much of the example code on the web forgets to include these HttpClients, for some reason 
     require_once('../docs/Facebook/HttpClients/FacebookHttpable.php'); 
     require_once('../docs/Facebook/HttpClients/FacebookCurl.php'); 
     require_once('../docs/Facebook/HttpClients/FacebookCurlHttpClient.php'); 
     require_once('../docs/Facebook/FacebookSession.php'); 
     require_once('../docs/Facebook/FacebookRedirectLoginHelper.php'); 
     require_once('../docs/Facebook/FacebookRequest.php'); 
     require_once('../docs/Facebook/FacebookResponse.php'); 

     require_once('../docs/Facebook/FacebookSDKException.php'); 
     require_once('../docs/Facebook/FacebookRequestException.php'); 
     require_once('../docs/Facebook/FacebookAuthorizationException.php'); 
     require_once('../docs/Facebook/GraphObject.php'); 

     // This one is also often left out 
     require_once('../docs/Facebook/Entities/AccessToken.php'); 

     // store your $HOSTNAME, $APPID and $SECRET in this file: 
     require_once('../docs/my_app_credentials.php'); 

     session_start(); 

     use Facebook\FacebookSession; 
     use Facebook\FacebookRedirectLoginHelper; 
     use Facebook\FacebookRequest; 
     use Facebook\FacebookResponse; 
     use Facebook\FacebookSDKException; 
     use Facebook\FacebookRequestException; 
     use Facebook\FacebookAuthorizationException; 
     use Facebook\GraphObject; 

     // init app with app id (APPID) and secret (SECRET) 
     FacebookSession::setDefaultApplication($APPID,$SECRET); 

     // login helper with redirect_uri 

     $PAGENAME="page2_upload_photo.php"; 
     $REDIRECT_URL="http://".$HOSTNAME.'/'.$PAGENAME; 
     $IMAGE_FILENAME='@images/Flower.jpg'; 
     $IMAGE_MESSAGE='hello flower'; 

     $helper = new FacebookRedirectLoginHelper($REDIRECT_URL); 
     echo "<p> the redirect url is ".$REDIRECT_URL."</p>"; 
     try { 
     // echo "<p> about to try to get session: the helper variable: </p>"; 
     // var_dump($helper); 
      $session = $helper->getSessionFromRedirect(); 
     // echo "<p> the session variable:</p>"; 
     // var_dump($session); 
      } 
     catch(FacebookRequestException $ex) { 
      // When Facebook returns an error 
      echo "<p> There was a facebook request exception</p>"; 
     } 
     catch(\Exception $ex) { 
      echo "<p> There was a validation failure</p>"; 
      // When validation fails or other local issues 
     } 
     // see if we have a session 
     if (isset($session)) { 
      echo "<p> Now try to upload a photo to the album /me</p>"; 
      try { 

       // Upload to a user's profile. The photo will be in the 
       // first album in the profile. You can also upload to 
       // a specific album by using /ALBUM_ID as the path 
       //$myCurlFile = new CURLFile('@./'.$IMAGE_FILENAME, 'image/jpg'); 
       $myPhoto = "@images/Flower.jpg"; 
       //var_dump($myCurlFile); 
       $response = (new FacebookRequest(
           $session, 'POST', '/me/photos', 
            array(
             'source'=>'$IMAGE_FILENAME', 
             'message'=>'$IMAGE_MESSAGE' 
             ) 
               ))->execute()->getGraphObject(); 

       // If you're not using PHP 5.5 or later, change the file reference to: 
       // 'source' => '@/path/to/file.name' 

       echo "Posted with id: " . $response->getProperty('id'); 

      } catch(FacebookRequestException $e) { 

      echo "Exception occurred, code: " . $e->getCode(); 
      echo " with message: " . $e->getMessage(); 

      } 

     } else { 
      // show login url 
      echo '<a href="' . $helper->getLoginUrl($scope) . '">Login</a>'; 
     } 

     ?> 
+0

檢查你的錯誤日誌。 – phwd 2015-02-08 17:19:52

+0

我的錯誤日誌中沒有任何內容顯示 – james 2015-02-08 17:23:09

+0

根據您的try/catch或照片帖子或您有錯誤。另一個路徑是你返回到getLoginUrl。你在每個地方都有一堆echo聲明,你應該能夠給出比'更具描述性的東西',我不確定我做錯了什麼或者我現在缺少什麼。我似乎無法在Facebook上發佈用戶的時間軸上的圖像。' – phwd 2015-02-08 17:32:53

回答

0

您尚未設置上傳文件。

$response = (new FacebookRequest(
           $session, 'POST', '/me/photos', 
            array(
             'source'=>'$IMAGE_FILENAME', 
             'message'=>'$IMAGE_MESSAGE' 
             ) 
               ))->execute()->getGraphObject(); 

您的報價,這將使它成爲一個字符串包圍的變量,因此源越來越文字串$ IMAGE_FILENAME

下面是一個簡單的例子

$IMAGE_FILENAME='@images/Flower.jpg'; 
$IMAGE_MESSAGE='hello flower'; 

$arr = array(
    'source'=>'$IMAGE_FILENAME', 
    'message'=>'$IMAGE_MESSAGE' 
); 


$correct_arr = array(
    'source'=>$IMAGE_FILENAME, 
    'message'=>$IMAGE_MESSAGE 
); 

print_r($arr); 

print_r($correct_arr); 

響應

Array 
(
    [source] => $IMAGE_FILENAME 
    [message] => $IMAGE_MESSAGE 
) 
Array 
(
    [source] => @images/Flower.jpg 
    [message] => hello flower 
) 
相關問題