Advertisement
Guest User

fbupload

a guest
Feb 10th, 2013
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.90 KB | None | 0 0
  1. <?php
  2. require_once 'library/facebook.php';
  3. $facebook = new Facebook(array(
  4.  'appId'  => '294175142205',
  5.  'secret' => '87df9b29d3dffaa24fd6036d75c1f668',
  6.  'fileUpload' => true
  7. ));
  8. ?>
  9. <html>
  10. <head>
  11. <title>WebSpeaks.in | Create album and upload photos to Facebook Fan Page</title>
  12. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  13. </head>
  14. <body>
  15. <?php
  16. //It can be found at https://developers.facebook.com/tools/access_token/
  17. $access_token = 'AAAAARH40cT0BANFh5V7Cn8dlXciZCbonSmGQZCeX3kUKIx2OMmvrqKUsHIBBj6bYi2vV25lMj6ZA5UDOaJpAxJXBEMOjZAcZD';
  18.  
  19. $params = array('access_token' => $access_token);
  20.  
  21. //The id of the fanpage
  22. $fanpage = '353686361320061';
  23.  
  24. /*
  25.  * Go to https://developers.facebook.com/tools/explorer?method=GET&path=me%2Faccounts
  26.  * Enter https://graph.facebook.com/me/accounts in the second text box
  27.  * Click on submit
  28.  * Json data will be returned
  29.  * From that select the access token of your Fan page
  30. */
  31. $page_access_token = 'AAACEdEose0cBANZASZChz2ygVvPvdGaZB1g1CD7FUqGf08XgGr4byZAPITZAAtTAFsq8HPMwsM7rGpIhZAXzSjxVCBU9AZCZAGimaRfCueeLTuHuj2OqHrm7gsgNlpRHrbwZD';
  32. $facebook->setAccessToken($page_access_token);
  33.  
  34. $facebook->setFileUploadSupport(true);
  35.  
  36. //Create an album
  37. $album_details = array(
  38.         'message'=> 'Test album',
  39.         'name'=> 'Album name'.date('Y-m-d H:i:s') //should be unique each time
  40. );
  41. $album = $facebook->api('/'.$fanpage.'/albums', 'post', $album_details);
  42.  
  43. //The id of the album
  44. $album_id =$album['id'];
  45.  
  46. //Replace arvind07 with your Facebook ID
  47. $accounts = $facebook->api('/arvind07/accounts', 'GET', $params);
  48.  
  49. foreach($accounts['data'] as $account) {
  50.     if( $account['id'] == $fanpage || $account['name'] == $fanpage ){
  51.         $fanpage_token = $account['access_token'];
  52.     }
  53. }
  54.  
  55. $valid_files = array('image/jpeg', 'image/png', 'image/gif');
  56.  
  57. if(isset($_FILES) && !empty($_FILES)){
  58.     if( !in_array($_FILES['pic']['type'], $valid_files ) ){
  59.         echo 'Only jpg, png and gif image types are supported!';
  60.     }else{
  61.         #Upload photo here
  62.         $img = realpath($_FILES["pic"]["tmp_name"]);
  63.  
  64.         $args = array(
  65.             'message' => 'This photo was uploaded via WebSpeaks.in',
  66.             'image' => '@' . $img,
  67.             'aid' => $album_id,
  68.             'no_story' => 0,
  69.             'access_token' => $fanpage_token
  70.         );
  71.  
  72.         $photo = $facebook->api($album_id . '/photos', 'post', $args);
  73.         if( is_array( $photo ) && !empty( $photo['id'] ) ){
  74.             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>';
  75.         }
  76.     }
  77. }
  78.  
  79. ?>
  80.  <!-- Form for uploading the photo -->
  81.  <div class="main">
  82.   <p>Select a photo to upload on Facebook Fan Page</p>
  83.   <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
  84.   <p>Select the image: <input type="file" name="pic" /></p>
  85.   <p><input class="post_but" type="submit" value="Create album and Upload" /></p>
  86.   </form>
  87.  </div>
  88. </body>
  89. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement