donkey199024 2015-02-08 17:18
浏览 27
已采纳

在Facebook用户的时间轴上发布图像

I have an app which uses the Facebook PHP API. This is part of my code ,I am not sure what i'm doing wrong or what i'm missing at the moment. I can't seem to post an image on the user's timeline on facebook. Any help would be much appreciated. Many Thanks.

        <?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>';
        }

        ?>
  • 写回答

1条回答 默认 最新

  • douxiajia6309 2015-02-08 18:35
    关注

    You haven't set the upload file.

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

    You enclosed the variables in quotes which will make it a string, so source is getting the literal string $IMAGE_FILENAME

    Here is a simple example

    $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);
    

    Response

    Array
    (
        [source] => $IMAGE_FILENAME
        [message] => $IMAGE_MESSAGE
    )
    Array
    (
        [source] => @images/Flower.jpg
        [message] => hello flower
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试