Advertisement
Altenator_IWnet

PHP with Facebook API - Thank to Ahlspiess TBD

Jul 16th, 2012
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.14 KB | None | 0 0
  1. <pre>
  2. <?php
  3. @ini_set('max_execution_time', 0);
  4. @set_time_limit(0);
  5.  
  6. class fb {
  7.    
  8.     var $FACEBOOK_DEVELOPER_PAGE = "https://developers.facebook.com/docs/reference/api/";
  9.     var $URL = "https://graph.facebook.com";
  10.     var $BASIC_URL = "/me";
  11.     var $NEWS_URL = "/me/home";
  12.     var $WALL_URL = "/me/feed";
  13.     var $FRIENDS_URL = "/me/friends";
  14.     var $LIKES_URL = "/me/likes";
  15.     var $MOVIES_URL = "/me/movies";
  16.     var $MUSIC_URL = "/me/music";
  17.     var $BOOKS_URL = "/me/books";
  18.     var $PERMISSIONS_URL = "/me/permissions";
  19.     var $PHOTOS_URL = "/me/photos";
  20.     var $ALBUMS_URL = "/me/albums";
  21.     var $VIDEOS_URL = "/me/videos";
  22.     var $EVENTS_URL = "/me/events";
  23.     var $GROUPS_URL = "/me/groups";
  24.     var $CHECKINS_URL = "/me/checkins";
  25.    
  26.     private $cookies, $access_token;
  27.     public $profile;
  28.    
  29.     function __construct($cookies) {
  30.         $this->cookies = $cookies;
  31.         $this->getFacebookGraphAccessToken();
  32.         $this->getProfileData();
  33.     }
  34.    
  35.     private function getFacebookGraphAccessToken() {
  36.         $conn = $this->connect($this->FACEBOOK_DEVELOPER_PAGE, 1, $this->cookies);
  37.         preg_match("/access_token=(\w+)/", $conn, $matches);
  38.         $this->access_token = $matches[1];
  39.     }
  40.    
  41.     private function getProfileData() {
  42.         $this->profile = json_decode($this->connect($this->URL.$this->BASIC_URL.'?access_token='.$this->access_token, 1, $this->cookies));
  43.     }
  44.    
  45.     function Profile() {
  46.         return $this->profile;
  47.     }
  48.    
  49.     function getPages() {
  50.         $ret = array();
  51.         $pg = json_decode($this->connect($this->URL.$this->LIKES_URL."?access_token=".$this->access_token, 1, $this->cookies));
  52.         $ret['total_page'] = count($pg->data);
  53.         $ret['page'] = $pg->data;
  54.         return $ret;
  55.     }
  56.    
  57.     function PageData($id) {
  58.         return json_decode($this->connect('https://graph.facebook.com/'.$id));
  59.     }
  60.    
  61.     function FriendData($id) {
  62.         return json_decode($this->connect('https://graph.facebook.com/'.$id));
  63.     }
  64.    
  65.     function getFriends() {
  66.         $ret = array();
  67.         $fr = json_decode($this->connect($this->URL.$this->FRIENDS_URL."?access_token=".$this->access_token, 1, $this->cookies));
  68.         $ret['total_friend'] = count($fr->data);
  69.         $ret['friend'] = $fr->data;
  70.         return $ret;
  71.     }
  72.    
  73.     function connect($url,
  74.                      $use_cookies = 0,
  75.                      $cookies = '',
  76.                      $ua = 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)') {
  77.                      
  78.         $conn = curl_init($url);
  79.         curl_setopt( $conn, CURLOPT_CONNECTTIMEOUT, 30 );
  80.         curl_setopt( $conn, CURLOPT_SSL_VERIFYPEER, false );
  81.         curl_setopt( $conn, CURLOPT_SSL_VERIFYHOST, 1 );
  82.         curl_setopt( $conn, CURLOPT_RETURNTRANSFER, true );
  83.         curl_setopt( $conn, CURLOPT_FOLLOWLOCATION, true );
  84.         curl_setopt( $conn, CURLOPT_USERAGENT, $ua );
  85.         if($use_cookies) {
  86.             curl_setopt( $conn, CURLOPT_COOKIE, $cookies );
  87.         }
  88.         curl_setopt( $conn, CURLOPT_POST, false );
  89.        
  90.         try {
  91.             $result = curl_exec( $conn );
  92.             curl_close( $conn );
  93.         } catch (Exception $ex) {
  94.             die("Connect: {$ex->getMessage()}");
  95.             return null;
  96.         }
  97.        
  98.         return $result;
  99.     }
  100. }
  101.  
  102.     $cookies = array(
  103.         "asiakarensri" => "datr=9Gz4Ta8-gIq5P8OlB644uWV_; lu=ggdjx4iJNAA3ZVYzJK65P89w; c_user=100000933666643; xs=1%3A277f05d24816e5944c4d09751b5da3b0%3A0%3A1325609978; p=1; act=1326182339325%2F83%3A2; presence=EDvFA22A2EtimeF1326195394EstateFDutF1326195394019EvisF0EvctF0H0EblcF0Es​ndF0OQ1326174217EsF0CEchFDp_5f1B00933666643F93CC",
  104.         "fredmontana" => "datr=-AziTuQ2iZAHJqXQSDHS0MM9; lu=SgrZuYpIZ6FAYyM0VOa-yBbg; c_user=1545069838; xs=61%3A83f9ba77dba340141a5c53ef11c4a036%3A0%3A1324883154; p=2; act=1325941538046%2F5%3A2; presence=EDvFA22A2EtimeF1326193229EstateFDutF1326193229356EvisF1EvctF0H0EblcF0Es​ndF1OQ1325941548EsF0CEchFDp_5f1545069838F0CC",
  105.         //"danielhunt" => "locale=en_GB; act=1326194800036%2F0%3A2; c_user=508119909; datr=q_ygTqhXnljJUAWciO1-16r2; lu=RgZ6VXya62pBSCqgZ_lix-rA; xs=60%3A7c306ceb65c6fc8fbc2a4c76727054c5%3A0%3A1326194835; p=0; wd=1024x499; presence=EDvFA22A2EtimeF1326194855EstateFDutF1326194855573EvisF1EvctF0H0EblcF0Es​ndF0OQ0EsF0CEchFDp_5f508119909F1CC",
  106.     );
  107.    
  108. $fb = new fb($cookies['asiakarensri']);
  109. $fbfriend = $fb->getFriends();
  110. $fbpage = $fb->getPages();
  111. ?>
  112. <style>
  113. table td{
  114.     background: #FFFEB2;
  115.     color: #0C0C0C;
  116.     border-bottom: 1px solid #FFED45;
  117.     border-right: 1px solid #FFED45;
  118.     padding: 3px;
  119. }
  120. table th{
  121.     padding: 5px;
  122.     background: #FFF064;
  123.     color: #060606;
  124. }
  125. </style>
  126. <table width='100%' border='0'>
  127.   <tr>
  128.    <td  colspan='2'>
  129.     <table width='100%' border='0'>
  130.      <tr>
  131.       <td width='10%' align="center"><img src="https://graph.facebook.com/<?=$fb->Profile()->id?>/picture"></td>
  132.       <td>
  133.       <table width='100%' border='0'>
  134.        <tr>
  135.          <td>
  136.           <a href="<?=$fb->Profile()->link?>"><?=$fb->Profile()->name?></a>
  137.          </td>
  138.        </tr>
  139.        <tr>
  140.          <td>
  141.           Gender: <?=$fb->Profile()->gender?>. Lives in <?=$fb->Profile()->location->name?>. Born on <?=$fb->Profile()->birthday?>, <?php if(!empty($fb->Profile()->relationship_status) AND isset($fb->Profile()->relationship_status)): ?>Relation Status <?=$fb->Profile()->relationship_status?><?php endif;?>
  142.          </td>
  143.        </tr>
  144.        <tr>
  145.         <td>About: <?=$fb->Profile()->about?></td>
  146.        </tr>
  147.       </table>
  148.       </td>
  149.     </tr>
  150.     </table>
  151.    </td>
  152.   </tr>
  153.   <tr>
  154.     <td width="50%">Total Page Like: <?=$fbpage['total_page']?>
  155.     <div style='overflow:auto; height:300px'>
  156.        <table width="100%" style='overflow:auto; height:300px'>
  157.                 <?php
  158.                 for($i=0;$i<$fbpage['total_page'];$i++) {
  159.                     //$pgdata = $fb->PageData($fbpage['page'][$i]->id);
  160.                     print "<tr>";
  161.                     print "<td align=\"center\"><img src=\"https://graph.facebook.com/".$fbpage['page'][$i]->id."/picture\"></td>";
  162.                     print "<td><a href=\"http://www.facebook.com/profile.php?id=".$fbpage['page'][$i]->id."\">".$fbpage['page'][$i]->name."</a></td>";
  163.                     print "</tr>\n";
  164.                 }
  165.                 ?>
  166.         </table>
  167.     </div>
  168.     </td>
  169.    <td>Total Friends: <?=$fbfriend['total_friend']?>.
  170.    <div style='overflow:auto; height:300px'>
  171.    <table width="100%" style='overflow:auto; height:300px'>
  172.             <?php
  173.             for($i=0;$i<$fbfriend['total_friend'];$i++) {
  174.                 //$frdata = $fb->FriendData($fbfriend['friend'][$i]->id);
  175.                 print "<tr>";
  176.                 print "<td align=\"center\"><img src=\"https://graph.facebook.com/".$fbfriend['friend'][$i]->id."/picture\"></td>";
  177.                 print "<td><a href=\"http://www.facebook.com/profile.php?id=".$fbfriend['friend'][$i]->id."\">".$fbfriend['friend'][$i]->name."</a></td>";
  178.                 print "</tr>\n";
  179.             }
  180.             ?>
  181.     </table>
  182.     </div>
  183.     </td>
  184.   </tr>
  185. </table>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement