Upload image to facebook fan page using API

前端 未结 4 880
温柔的废话
温柔的废话 2021-02-04 14:50

I am trying to upload an image through my application with Facebook Graph API to an album of my fan page. Although I provide the albumID like a parameter for uploading the image

4条回答
  •  花落未央
    2021-02-04 15:16

    Here is the script for uploading photos on your Facebook Fanpage:

    
    
    WebSpeaks.in | Upload images to Facebook
    
    
    
    
     $app_id,,
     'secret' => $app_secret,
     'fileUpload' => true
    ));
    
    
    //It can be found at https://developers.facebook.com/tools/access_token/
    $access_token = '';
    
    $params = array('access_token' => $access_token);
    
    //The id of the fanpage
    $fanpage = '330299184793';
    
    //The id of the album
    $album_id ='10150418901414794';
    
    //Replace arvind07 with your Facebook ID
    $accounts = $facebook->api('/arvind07/accounts', 'GET', $params);
    
    foreach($accounts['data'] as $account) {
     if( $account['id'] == $fanpage || $account['name'] == $fanpage ){
      $fanpage_token = $account['access_token'];
     }
    }
    
    
    $valid_files = array('image/jpeg', 'image/png', 'image/gif');
    
    if(isset($_FILES) && !empty($_FILES)){
     if( !in_array($_FILES['pic']['type'], $valid_files ) ){
      echo 'Only jpg, png and gif image types are supported!';
     }else{
      #Upload photo here
      $img = realpath($_FILES["pic"]["tmp_name"]);
    
      $args = array(
       'message' => 'This photo was uploaded via WebSpeaks.in',
       'image' => '@' . $img,
       'aid' => $album_id,
       'no_story' => 1,
       'access_token' => $fanpage_token
      );
    
      $photo = $facebook->api($album_id . '/photos', 'post', $args);
      if( is_array( $photo ) && !empty( $photo['id'] ) ){
       echo '

    Click here to watch this photo on Facebook.

    '; } } } ?>

    Select a photo to upload on Facebook Fan Page

    Select the image:

    You can watch complete tutorial here.

提交回复
热议问题