用php将照片上传到facebook粉丝页面墙

时间:2013-08-28 18:19:46

标签: php facebook facebook-graph-api

tutorial帮助我编写了以下代码;有了它,我可以将照片上传到我的粉丝页面上的相册。如何制作它以便我可以发布到粉丝页面墙而不仅仅是将照片上传到相册?我正在考虑在下面实现所需的代码。

<?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>

1 个答案:

答案 0 :(得分:0)

实际上未发布的原因是由于我之前的代码中使用no_story=1 they say in this other stack question