2015-02-08 24 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 
) 
相关问题