Advertisement
Guest User

PHP event create

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