Advertisement
Guest User

php script with latest version fb php sdk

a guest
Mar 9th, 2015
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.38 KB | None | 0 0
  1. <?php
  2. //error_reporting(E_ALL);
  3. //ini_set('display_errors','On');
  4. //ini_set('display_errors', '1');
  5. require '../../vendor/autoload.php';
  6.  
  7. use Facebook\FacebookSession;
  8. use Facebook\FacebookRedirectLoginHelper;
  9. use Facebook\FacebookCanvasLoginHelper;
  10. use Facebook\FacebookRequest;
  11. use Facebook\FacebookResponse;
  12. use Facebook\FacebookSDKException;
  13. use Facebook\FacebookRequestException;
  14. use Facebook\FacebookAuthorizationException;
  15. use Facebook\GraphUser;
  16. use Facebook\GraphObject;
  17. use Facebook\Entities\AccessToken;
  18. use Facebook\HttpClients\FacebookCurlHttpClient;
  19. use Facebook\HttpClients\FacebookHttpable;
  20.  
  21. $fetchUserData = new FetchUserData();
  22.  
  23. class FetchUserData
  24. {
  25.     //holds the users uid
  26.     public $id;
  27.    
  28.     public $selectionUID;
  29.    
  30.     //holds a reference to the db connection
  31.     public $connection;
  32.    
  33.     //the output object to send back to flash
  34.     public $outputObject;
  35.    
  36.     //a reference to thd facebook object
  37.     public $facebook;
  38.    
  39.     public $session;
  40.    
  41.     public $currentUserObject;
  42.    
  43.     public function __construct()
  44.     {
  45.         //include the database connection
  46.         include 'database.php';
  47.        
  48.         //initialize the output object
  49.         $this->outputObject = new stdClass();
  50.        
  51.         //include the php sdk
  52.         //include the facebook sdk library
  53. //      require_once('facebook.php');
  54. //     
  55. //      //instantiate a new Facebook Object, you must replace your APP_ID with your real apps app id. do the same for the secret.
  56. //      $this->facebook = new Facebook(array(
  57. //              'appId' =>  '123',
  58. //              'secret'    =>  '123',
  59. //              'cookie'    =>  true));
  60.         FacebookSession::setDefaultApplication('123', '123');
  61.            
  62.             $helper = new FacebookCanvasLoginHelper();
  63.         try {
  64.           $this->session = $helper->getSession();
  65.         } catch(FacebookRequestException $ex) {
  66.           // When Facebook returns an error
  67.           $this->outputObject->loggedIn = "facebook error";
  68.         } catch(\Exception $ex) {
  69.           // When validation fails or other local issues
  70.           $this->outputObject->loggedIn = "validation failed";
  71.         }
  72.         if ($this->session) {
  73.             // Logged in
  74.             $this->outputObject->loggedIn = "true";
  75.             /* make the API call */
  76.             $request = new FacebookRequest(
  77.               $this->session,
  78.               'GET',
  79.               '/me/?fields=id,first_name,last_name,picture'
  80.             );
  81.         }
  82.         $response1 = $request->execute();
  83.         //$graphObject = $response->getGraphObject();
  84.         /* handle the result */
  85.         //$this->outputObject->ricky1 = json_encode(var_dump($response));
  86.         //$this->outputObject->ricky2 = json_encode(var_dump($graphObject));
  87.         $this->currentUserObject = json_decode($response1->getRawResponse());
  88.        
  89.         //$this->outputObject->ricky2 = json_encode($jsonObject);
  90.         //$user_profile = (new FacebookRequest($this->session, 'GET', '/me'))->execute()->getGraphObject(GraphUser::className());
  91.  
  92.         //$this->outputObject->name = $user_profile->getId();
  93.         $this->id = mysqli_real_escape_string($connection, $this->currentUserObject->id);
  94.        
  95.         //attempt to get the current user
  96. //      $this->id = mysqli_real_escape_string($connection, $this->facebook->getUser());
  97.        
  98.         //store a reference to the db connection in this object
  99.         $this->connection = $connection;
  100.        
  101.         //get the passed in selection UID
  102.         $this->selectionUID = $this->getSelectionUID();
  103.        
  104.         //only if a selection uid was received continue
  105.         if($this->selectionUID != NULL)
  106.         {
  107.             $this->retrieveUsersDataFromDatabase($this->selectionUID);
  108.            
  109.             $this->fetchDrawings();
  110.         }
  111.         echo json_encode($this->outputObject);
  112.     }
  113.     public function fetchDrawings()
  114.     {
  115.         //craft up a query
  116.         $query = "SELECT * FROM drawings WHERE victimUID = '" . $this->selectionUID . "'";
  117.        
  118.         //execute the query
  119.         $result = mysqli_query($this->connection, $query);
  120.        
  121.         //create an output array
  122.         $outputArray = array();
  123.        
  124.         while($row = mysqli_fetch_assoc($result))
  125.         {
  126.             array_push($outputArray, $row);
  127.         }
  128.        
  129.         //loop through the output array to get the first_name, last_name, pictureURL from facebook graph api
  130.         for($i = 0; $i < count($outputArray); $i++)
  131.         {
  132.             $id = mysqli_real_escape_string($this->connection, $outputArray[$i]['artistUID']);
  133.            
  134.             if($id == $this->id)
  135.             {
  136.                 $id = "me";
  137.                
  138.                 $jsonObject = $this->currentUserObject;
  139.             } else
  140.             {
  141.                 /* make the API call */
  142.                 $request = new FacebookRequest(
  143.                   $this->session,
  144.                   'GET',
  145.                   '/' . $id . '/?fields=id,first_name,last_name,picture'
  146.                 );
  147.                
  148.                 $response1 = $request->execute();
  149.                 //$graphObject = $response->getGraphObject();
  150.                 /* handle the result */
  151.                 //$this->outputObject->ricky1 = json_encode(var_dump($response));
  152.                 //$this->outputObject->ricky2 = json_encode(var_dump($graphObject));
  153.                 $jsonObject = json_decode($response1->getRawResponse());
  154.             }
  155.            
  156.            
  157.            
  158.             //$this->outputObject->ricky2 = json_encode($jsonObject);
  159.             //$user_profile = (new FacebookRequest($this->session, 'GET', '/me'))->execute()->getGraphObject(GraphUser::className());
  160.    
  161.             //$this->outputObject->name = $user_profile->getId();
  162.             //$this->id = mysqli_real_escape_string($connection, $jsonObject->id);
  163.            
  164.             //connect to the graph api to fetch the users first_name, last_name, and picture
  165.             //$user = $this->facebook->api($id . "/?fields=id,first_name,last_name,picture,gender,timezone,link");
  166.            
  167.             $outputArray[$i]['uid'] = $jsonObject->id;
  168.             $outputArray[$i]['first_name'] = $jsonObject->first_name;
  169.             $outputArray[$i]['last_name'] = $jsonObject->last_name;
  170.             $outputArray[$i]['pictureURL'] = $jsonObject->picture->data->url;
  171.             //$outputArray[$i]['gender'] = $jsonObject->gender;
  172. //          $outputArray[$i]['timezone'] = $jsonObject->timezone;
  173. //          $outputArray[$i]['link'] = $jsonObject->link;
  174.            
  175.             //connect to the database to get the users faveorite colors
  176.             //craft up a query
  177.             $query = "SELECT favColor1,favColor2,motd2 FROM Users WHERE uid='" . $id . "' LIMIT 1";
  178.            
  179.             //$this->outputObject->query = $query;
  180.            
  181.             //execute the query
  182.             $result = mysqli_query($this->connection, $query);
  183.            
  184.             while($row = mysqli_fetch_assoc($result))
  185.             {
  186.                 //$this->outputObject->testing = $row['favColor1'] . " " . $row['favColor2'];
  187.                 $outputArray[$i]['favColor1'] = $row['favColor1'];
  188.                 $outputArray[$i]['favColor2'] = $row['favColor2'];
  189.                 $outputArray[$i]['motd2'] = $row['motd2'];
  190.             }
  191.         }
  192.        
  193.         //store a reference to the array in the output object
  194.         $this->outputObject->drawingsData = $outputArray;
  195.     }
  196.     public function retrieveUsersDataFromDatabase($uid)
  197.     {
  198.         //prepare a query
  199.         $query = "SELECT * FROM Users WHERE uid in (" . $uid . ") LIMIT 1;";
  200.        
  201.         //execute the query
  202.         $result = mysqli_query($this->connection, $query);
  203.        
  204.         while($row = mysqli_fetch_assoc($result))
  205.         {
  206.             $this->outputObject->userData = $row;
  207.         }
  208.        
  209.         if($uid == $this->id)
  210.         {
  211.             $uid = "me";
  212.            
  213.             $jsonObject = $this->currentUserObject;
  214.         } else
  215.         {
  216.             /* make the API call */
  217.             $request = new FacebookRequest(
  218.               $this->session,
  219.               'GET',
  220.               '/' . $uid . '/?fields=id,first_name,last_name,picture'
  221.             );
  222.            
  223.             $response1 = $request->execute();
  224.             //$graphObject = $response->getGraphObject();
  225.             /* handle the result */
  226.             //$this->outputObject->ricky1 = json_encode(var_dump($response));
  227.             //$this->outputObject->ricky2 = json_encode(var_dump($graphObject));
  228.             $jsonObject = json_decode($response1->getRawResponse());
  229.         }
  230.        
  231.        
  232.        
  233.         //$this->outputObject->ricky2 = json_encode($jsonObject);
  234.         //$user_profile = (new FacebookRequest($this->session, 'GET', '/me'))->execute()->getGraphObject(GraphUser::className());
  235.  
  236.         //$this->outputObject->name = $user_profile->getId();
  237.         //$this->id = mysqli_real_escape_string($connection, $jsonObject->id);
  238.        
  239.         //fetch the users pictureURL from facebook graph api
  240.         //$user = $this->facebook->api("/" . $uid . "?fields=id,first_name,last_name,picture,gender,timezone,link");
  241.        
  242.         $this->outputObject->userData['uid'] = $jsonObject->id;
  243.         $this->outputObject->userData['first_name'] = $jsonObject->first_name;
  244.         $this->outputObject->userData['last_name'] = $jsonObject->last_name;
  245.         $this->outputObject->userData['pictureURL'] = $jsonObject->picture->data->url;
  246.         //$this->outputObject->userData['gender'] = $jsonObject->gender;
  247. //      $this->outputObject->userData['timezone'] = $jsonObject->timezone;
  248. //      $this->outputObject->userData['link'] = $user['link'];
  249.     }
  250.     public function getSelectionUID()
  251.     {
  252.         if(isset($_POST['selected_uid']))
  253.         {
  254.             return mysqli_real_escape_string($this->connection, $_POST['selected_uid']);
  255.         } else
  256.         {
  257.             return NULL;
  258.         }
  259.     }
  260.  
  261. }
  262.  
  263. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement