Want more features on Pastebin? Sign Up, it's FREE!
Guest

Upload picture to facebook fan page using Graph API

By: swissspidy on Jul 20th, 2011  |  syntax: PHP  |  size: 1.75 KB  |  views: 5,508  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. // Create our FB Application instance
  3. require_once('facebook/facebook.php');
  4. $facebook = new Facebook(array(
  5.         'appId'  => '19xxxxxxxxxx834',
  6.         'secret' => '5bxxxxxxxxxxxxxxxxxxxxxxxxxxxxx0',
  7.         'fileUpload' => true
  8. ));
  9.  
  10. // Access token (previously got from https://developers.facebook.com/tools/access_token/)
  11. $access_token = '19551xxxxxxxxxxxxxxxxxxwc';
  12.  
  13. // Default params for all Graph API requests
  14. $params = array(
  15.         'access_token' => $access_token
  16. );
  17.  
  18. // The id of the fanpage
  19. $fanpage = '20xxxxxxxxxxx69';
  20.  
  21. // Get the page access token
  22. $accounts = $facebook->api('/my_account_id/accounts', 'GET', $params);
  23. $data = $accounts['data'];
  24. foreach($data as $account) {
  25.         if( $account['id'] == $fanpage || $account['name'] == $fanpage )
  26.                 $fanpage_token = $account['access_token'];
  27. }
  28.  
  29. // Get all albums from the page
  30. // Must use app access token, not page token!
  31. // You can also use a static album id to test
  32. $fanpage_albums = $facebook->api($fanpage . '/albums', 'GET', $params);
  33. $albums = $fanpage_albums['data'];
  34. $sorted = array();
  35. foreach($albums as $album) {
  36.         if( ! strpos($album['name'], 'Special') )
  37.                 continue;
  38.         $sorted[] = $album;
  39. }
  40. $album_id = $sorted[0]['id']; // Get the first one. Shouldn't be empty!
  41.  
  42. // Upload the photo (previously uploaded by user)
  43. $args = array(
  44.         'message' => 'Von ' . $teilnehmer_name,
  45.         'image' => '@' . realpath($path. $_FILES['media']['name']),
  46.         'aid' => $album_id,
  47.         'no_story' => 1 // Nicht auf der Wall anzeigen (Thank God for that),
  48.         'access_token' => $fanpage_token // note, we use the page token here
  49. );
  50.  
  51. $photo = $facebook->api($album_id . '/photos', 'post', $args);
  52. if( is_array( $photo ) && ! empty( $photo['id'] ) )
  53.         echo 'Photo uploaded. Check it on Graph API Explorer. ID: ' . $photo['id'];
clone this paste RAW Paste Data