Facebook PHP SDK app to upload photo to page

安稳与你 提交于 2019-12-04 04:21:21

问题


I am trying to create a Facebook application that uploads images to a page. I can manage to upload photos to the user's photo album but not to the page's.

Here's my code:

require_once('php-sdk/facebook.php');

 $config = array(
  'appId' => 'XXXXXXXX',
  'secret' => 'YYYYYYYYYYYYYYYY',
  'fileUpload' => true,
 );

$facebook = new Facebook($config);
$user_id = $facebook->getUser();    

$photo = $_POST['image'];
$message = "Lorem ipsum";

echo $user_id;

  if ($user_id) {
       $ret_obj = $facebook->api('/XXXXXXXXXX/photos', 'POST', array(
        'access_token'  => $access_token,
                    'source' => '@' . $photo,
                    'message' => $message
                                    )
                                  );

} catch(FacebookApiException $e) {

    $login_url = $facebook->getLoginUrl( array(
                   'scope' => 'user_photos,publish_stream,photo_upload'
                   )); 
         echo 'Please <a href="' . $login_url . '">login.</a>';
    print_r($e);
            error_log($e->getType());
            error_log($e->getMessage());
  }   
  echo '<br /><a href="' . $facebook->getLogoutUrl() . '">logout</a>';
 } else {

  $login_url = $facebook->getLoginUrl( array( 'scope' =>user_photos,publish_stream,photo_upload,manage_pages' ) );
     echo 'Please <a href="' . $login_url . '">login.</a>';

}

Any help would be much appreciated.


回答1:


When you do:

$facebook->api('/XXXXXXXXXX/photos', 'POST', array(

... is XXXXXXXXXX your appId or the albumId? You must do the call to your album ID.

Also make sure you are using the correct $access_token. To make sure look at:

https://graph.facebook.com/yourPageID?fields=access_token&access_token=YourPersonalAccessToken

where yourPersonalAccessToken is:

A Page admin access_token for this page; The current user must be an administrator of this page; only returned if specifically requested via the fields URL parameter.

Your must grant to graph api manage_pages permissions.



来源:https://stackoverflow.com/questions/11227369/facebook-php-sdk-app-to-upload-photo-to-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!