Advertisement
Guest User

Facebook Graph API - Create event with image

a guest
Nov 22nd, 2010
1,220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.28 KB | None | 0 0
  1. <?php
  2.     require 'facebook.php';
  3.  
  4.     $base_url  =   "http://promos.uk.glam.com/splash_test/example.php";
  5.    
  6.     // Create our Application instance.
  7.     $facebook = new Facebook(array(
  8.       'appId'  => 'XXXXXXXXXXXXXXXXXXXXXX',
  9.       'secret' => 'XXXXXXXXXXXXXXXXXXXXXX',
  10.       'cookie' => true,
  11.       'fileUpload' => true
  12.     ));
  13.    
  14.     function debug($e, $name = '') {
  15.         echo '<p><pre>'.$name.' : '; print_r($e); echo '</pre></p>';
  16.     }
  17.  
  18.     // We may or may not have this data based on a $_GET or $_COOKIE based session.
  19.     //
  20.     // If we get a session here, it means we found a correctly signed session using
  21.     // the Application Secret only Facebook and the Application know. We dont know
  22.     // if it is still valid until we make an API call using the session. A session
  23.     // can become invalid if it has already expired (should not be getting the
  24.     // session back in this case) or if the user logged out of Facebook.
  25.     $session = $facebook->getSession();
  26.    
  27.     $me = null;
  28.     // Session based API call.
  29.     if ($session) {
  30.       try {
  31.         $uid = $facebook->getUser();
  32.         $me = $facebook->api('/me');
  33.         debug($facebook->api('/me'), 'Me');
  34.         debug($facebook, 'FB');
  35.        
  36.         //We seem to have a valid session, lets post the evetn
  37.       } catch (FacebookApiException $e) {
  38.         debug($e);
  39.       }
  40.     }
  41.  
  42.     // login or logout url will be needed depending on current user state.
  43.     if ($me) {
  44.       $logoutUrl = $facebook->getLogoutUrl(array('next' => $base_url));
  45.     } else {
  46.       $loginUrl = $facebook->getLoginUrl(
  47.         array(
  48.             'display'   => 'popup',
  49.             'next'      => $base_url . '?loginsucc=1',
  50.             'cancel_url'=> $base_url . '?cancel=1',
  51.             'req_perms' => 'create_event')
  52.         );
  53.     }
  54.  
  55.     // if user click cancel in the popup window
  56.     if (isset($_REQUEST['cancel'])) {
  57.         die("<script>
  58.            window.close();
  59.            </script>");
  60.    } elseif($me && isset($_REQUEST['loginsucc'])) {
  61.         //only if valid session found and loginsucc is set
  62.  
  63.         //after facebook redirects it will send a session parameter as a json value
  64.         //now decode them, make them array and sort based on keys
  65.         $sortArray = get_object_vars(json_decode($_GET['session']));
  66.         ksort($sortArray);
  67.  
  68.         $strCookie  =   "";
  69.         $flag       =   false;
  70.         foreach($sortArray as $key=>$item){
  71.             if ($flag) $strCookie .= '&';
  72.             $strCookie .= $key . '=' . $item;
  73.             $flag = true;
  74.         }
  75.  
  76.         //now set the cookie so that next time user don't need to click login again
  77.         setCookie('fbs_339fceae2abcaf1d95f324229e7b58a0', $strCookie);
  78.  
  79.         die("<script>
  80.            window.close();
  81.            window.opener.location.reload();
  82.            </script>");
  83.     }
  84. ?>
  85. <!DOCTYPE html>
  86. <html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
  87.     <head>
  88.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
  89.         <title>Create an event on Facebook</title>
  90.     </head>
  91. <body>
  92. <?php
  93.     //if user is logged in and session is valid.
  94.     if ($me){
  95.        
  96.         try {
  97.              //Path to photo (only tested with relative path to same directory)
  98.             $file = "end300.jpg";
  99.             //The event information array (timestamps are "Facebook time"...)
  100.             $event_info = array(
  101.                 "privacy_type" => "SECRET",
  102.                 "name" => "Event Title",
  103.                 "host" => "Me",
  104.                 "start_time" => 1290790800,
  105.                 "end_time" => 1290799800,
  106.                 "location" => "London",
  107.                 "description" => "Event Description"
  108.             );
  109.             //The key part - The path to the file with the CURL syntax
  110.             $event_info[basename($file)] = '@' . realpath($file);
  111.             //Make the call - returns the event ID
  112.             debug($facebook->api('me/events','post',$event_info), "THE EVENT");
  113.            
  114.             //debug($facebook->api($call), 'call');
  115.  
  116.         } catch (FacebookApiException $e) {
  117.             debug($e);
  118.         }
  119.            
  120.         echo '<a href="' . $facebook->getLogoutUrl() . '">Logout</a>';
  121.     } else {
  122.         ?>
  123.         <script type="text/javascript">
  124.         var newwindow;
  125.         var intId;
  126.         function login(){
  127.             var  screenX    = typeof window.screenX != 'undefined' ? window.screenX : window.screenLeft,
  128.                  screenY    = typeof window.screenY != 'undefined' ? window.screenY : window.screenTop,
  129.                  outerWidth = typeof window.outerWidth != 'undefined' ? window.outerWidth : document.body.clientWidth,
  130.                  outerHeight = typeof window.outerHeight != 'undefined' ? window.outerHeight : (document.body.clientHeight - 22),
  131.                  width    = 500,
  132.                  height   = 270,
  133.                  left     = parseInt(screenX + ((outerWidth - width) / 2), 10),
  134.                  top      = parseInt(screenY + ((outerHeight - height) / 2.5), 10),
  135.                  features = (
  136.                     'width=' + width +
  137.                     ',height=' + height +
  138.                     ',left=' + left +
  139.                     ',top=' + top
  140.                   );
  141.  
  142.             newwindow=window.open('<?=$loginUrl?>','Login by facebook',features);
  143.  
  144.              if (window.focus) {newwindow.focus()}
  145.             return false;
  146.         }
  147.         </script>
  148.         Please login to Facebook and we will setup the event for you!
  149.         <a href="#" onclick="login();return false;">
  150.             <img src="http://static.ak.fbcdn.net/rsrc.php/zB6N8/hash/4li2k73z.gif" border="0">
  151.         </a>
  152.         <?php
  153.     }
  154. ?>
  155.     </body>
  156. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement