简体   繁体   中英

Upload photo to facebook fan page wall with php

This tutorial has helped me writing the following code; with it I am able to upload a photo to an album on my fan page. How can I make it so that I can publish to the fan page wall and not just upload the photo to the album? I am looking in implementing the required code within this below.

<?php
  error_reporting(E_ALL);
  ini_set('display_errors', '1');
  require_once 'library/facebook.php';
  $facebook = new Facebook(array(
   'appId'  => 'My APP ID',
   'secret' => 'My Secret KEY',
   'fileUpload' => true
  ));
  #It can be found at https://developers.facebook.com/tools/access_token/
  #Change to your token.
  $access_token = 'My access token';
  $params = array('access_token' => $access_token);
  #The id of the fanpage
  $fanpage = 'My Fan Page ID';
  #The id of the album
  $album_id ='My Album ID';
  $accounts = $facebook->api('/My User Name/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{
       $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 '<p><a target="_blank" href="http://www.facebook.com/photo.php?fbid='.$photo['id'].'">Click here to watch this photo on Facebook.</a></p>';
       }
   }
 }
?>

<div class="main">
    <p>Select a photo to upload on Facebook Fan Page</p>
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
    <p>Select the image: <input type="file" name="pic" /></p>
    <p><input class="post_but" type="submit" value="Upload to my album" /></p>
    </form>
</div>

实际上它没有发布的原因是因为在我之前的代码中使用了no_story=1 ,正如他们在另一个堆栈问题中所说的那样

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM