Facebook API sdk 4.0 - post a photo onto facebook

前端 未结 2 1452
遥遥无期
遥遥无期 2021-01-26 11:25

I am trying to create an application where the user can browse and submit a photo from their computer onto their facebook. For this, they will first have to upload their photo o

相关标签:
2条回答
  • 2021-01-26 12:16

    OK, there are two main issues with your code.

    FIRST:

    Remove session_destroy() from your code. It's the reason why the session is being deleted. You can then remove the second use of $session = new FacebookSession( $_SESSION['fb_token']); from your code. You only need to do that bit once!

    Change:

    echo '<a href="' . $helper->getLogoutUrl( $session, 'http://localhost/app/index.php', session_destroy() ) .    '">Logout</a>';
    

    to

    echo '<a href="' . $helper->getLogoutUrl( $session, 'http://localhost/app/index.php' ) .    '">Logout</a>';
    

    SECOND:

    Append @ to your source and you will find that the image uploads correctly. E.g.:

    $request = new FacebookRequest(
      $session,
      'POST',
      '/me/photos',
      array (
        'source' => new CURLFile( $location.$name ),
      )
    );
    

    CURLFile only works on PHP 5.5+. For earlier version of PHP, set source to:

    'source' => '@' . $location.$name
    
    0 讨论(0)
  • 2021-01-26 12:22

    Assuming that the user has correctly uploaded the image and $location.$name is the path:

     $request = new FacebookRequest(
         $session,
         'POST',
         '/me/photos',
         array (
            'source' => file_get_contents($location.$name),
         )
      );
    
     $response = $request->execute();
     $graphObject = $response->getGraphObject();
    

    Or another possibility is via URL:

     $request = new FacebookRequest(
       $session,
       'POST',
       '/me/photos',
       array (
         'url' => $the_url_to_the_image,
       )
     );
    
     $response = $request->execute();
     $graphObject = $response->getGraphObject();
    

    And the API call you do AFTER the upload is done by the user but that is not more than logical.

    0 讨论(0)
提交回复
热议问题