Advertisement
terorama

PHP Examples 4

Oct 13th, 2012
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 13.26 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Copyright 2011 Facebook, Inc.
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License"); you may
  6.  * not use this file except in compliance with the License. You may obtain
  7.  * a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13.  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14.  * License for the specific language governing permissions and limitations
  15.  * under the License.
  16.  */
  17.  
  18. require 'facebook.php';
  19.  
  20. ini_set('display_errors','1');
  21. error_reporting (E_ALL);
  22.  
  23.  
  24. // Create our Application instance (replace this with your appId and secret).
  25. $facebook = new Facebook(array(
  26.   'appId'  => '',
  27.   'secret' => '',
  28. ));
  29.  
  30. // Get User ID
  31. $user = $facebook->getUser();
  32.  
  33. // We may or may not have this data based on whether the user is logged in.
  34. //
  35. // If we have a $user id here, it means we know the user is logged into
  36. // Facebook, but we don't know if the access token is valid. An access
  37. // token is invalid if the user logged out of Facebook.
  38.  
  39.  
  40.  
  41. // Login or logout url will be needed depending on current user state.
  42. if ($user)
  43. {
  44.   $logoutUrl = $facebook->getLogoutUrl();
  45. }
  46. else
  47. {
  48.   $loginUrl = $facebook->getLoginUrl(array('scope'=>
  49.   'user_about_me,
  50.  user_activities,
  51.  user_birthday,
  52.  user_checkins,
  53.  user_education_history,
  54.  user_events,
  55.  user_groups,
  56.  user_hometown,
  57.  user_interests,
  58.  user_likes,
  59.  user_location,
  60.  user_notes,
  61.  user_photos,
  62.  user_questions,
  63.  user_relationships,
  64.  user_relationship_details,
  65.  user_religion_politics,
  66.  user_status,
  67.  user_subscriptions,
  68.  user_videos,
  69.  user_website,
  70.  user_work_history,
  71.  email,
  72.  
  73.  read_friendlists,
  74.  read_insights,
  75.  read_mailbox,
  76.  read_requests,
  77.  read_stream,
  78.  xmpp_login,
  79.  ads_management,
  80.  create_event,
  81.  manage_friendlists,
  82.  manage_notifications,
  83.  user_online_presence,
  84.  friends_online_presence,
  85.  publish_checkins,
  86.  publish_stream,
  87.  rsvp_event
  88.  '));
  89. }
  90. //-----------------------------------------------------
  91. function some_wtf() {
  92.  
  93.  
  94.     $object_id = array(); //заполняем массив объектов c отметкой like
  95.         $i=0;
  96.         //$fql='SELECT user_id FROM like WHERE object_id=415946355119791';
  97.         //$fql.='.$object_id[6].';
  98.         foreach ($user_likes_data as $array1)
  99.         {
  100.             $object_id[$i]=$array1['id'];
  101.             //$fql.='.$object_id[$i].';
  102.             ++$i;
  103.         }
  104.         //узнаем всех пользователей, которым нравится те же объекты, что и мне
  105.  
  106.         $fql='SELECT user_id FROM like WHERE object_id=415946355119791';
  107.         $param=array(
  108.                     'method'     => 'fql.query',
  109.                     'query'     => $fql,
  110.                     'callback'    => ''
  111.                     );
  112.         $fql_result1= $facebook->api($param);
  113.         echo '<pre>';
  114.         print_r($fql_result1);
  115.         echo '</pre>';
  116.    
  117. }
  118.  
  119. //-----------------------------------------------------
  120.  
  121. function collectInfo() {
  122.  
  123.    global $facebook;
  124.    global $user;
  125.    
  126.    
  127.    if ($user)
  128.    {
  129.      try
  130.      {
  131.        // Proceed knowing you have a logged in user who's authenticated.
  132.        ob_start();
  133.        $user_profile = $facebook->api('/me','GET'); //получаем информацию о профиле пользователя
  134.        //$friends = $facebook->api('/me/friends'); // получаем список друзей текущего пользователя
  135.        $user_likes = $facebook->api('/me/likes');
  136.        echo 'user likes<br/>';
  137.        echo '<pre>'.print_r($user_likes,true).'</pre>';
  138.        
  139.        $inf = ob_get_contents();
  140.        ob_end_clean();
  141.        return $inf;
  142.    
  143.    } catch (FacebookApiException $e)
  144.      {
  145.        return false;
  146.      }
  147.    }
  148.    else
  149.       return false;  
  150.    
  151.    
  152. }
  153. //-----------------------------------------------------
  154.  
  155. ?>
  156.  
  157. <!doctype html>
  158. <html xmlns:fb="http://www.facebook.com/2008/fbml">
  159.   <head>
  160.     <title>php-sdk</title>
  161.     <style>
  162.       body {
  163.         font-family: 'Lucida Grande', Verdana, Arial, sans-serif;
  164.       }
  165.       h1 a {
  166.         text-decoration: none;
  167.         color: #3b5998;
  168.       }
  169.       h1 a:hover {
  170.         text-decoration: underline;
  171.       }
  172.     </style>
  173.   </head>
  174.   <body>
  175.     <h1>php-sdk</h1>
  176.  
  177.     <?php if ($user): ?>
  178.       <a href="<?php echo $logoutUrl; ?>">Logout</a>
  179.     <?php else: ?>
  180.       <div>
  181.         Login using OAuth 2.0 handled by the PHP SDK:
  182.         <a href="<?php echo $loginUrl; ?>">Login with Facebook</a>
  183.       </div>
  184.     <?php endif ?>
  185.    
  186.     <?php
  187.        $cdir = opendir('');
  188.        while($inf=readdir($cdir)) {
  189.           if (substr($inf,0,5)=='uinfo') {
  190.              echo "<a href=\"$inf\" target=\"_blank\">$inf</a><br/>";
  191.           }
  192.        }
  193.        closedir($cdir);
  194.        
  195.        
  196.        $rez = collect_info();
  197.        
  198.        if ($rez!==false) {
  199.           $fname='uinfo_'.date('d_m_Y_H_i_s').rand(6,8000).'.html';
  200.           $fh = fopen($fname,'w');
  201.           fwrite($fh, $rez);
  202.           fclose($fh);
  203.          
  204.           echo "<h3>You</h3><img src=\"https://graph.facebook.com/$user/picture\" /><br/>";
  205.           echo "user information collected in: <a href=\"$fname\" target=\"_blank\">$fname</a><br/>";
  206.        }
  207.        
  208.     ?>
  209.    
  210.    
  211.  
  212.     <h3>PHP Session</h3>
  213.     <pre><?php print_r($_SESSION); ?></pre>
  214.    
  215.  
  216.   </body>
  217. </html>
  218.  
  219. //---------------------------------------------------------------------------------------
  220. //---------------------------------------------------------------------------------------
  221. <?php
  222.  
  223.    $s='https://graph.facebook.com/zefirmedia/feed?access_token=AAAAAAITEghMBAA57e5imZBkZAZCEgooOgL4qkBOokGkFvfsBbUZADHe7YXzO7MxZCYWlVY3SKEZBUEVKHgGWHuGUA23yu3HSGREkyDMQByZAQs4czhBWV6ZB';
  224.  
  225.    /*$inf = file_get_contents($s);
  226.    echo $inf;
  227.    $fh = fopen('fb.txt','w');
  228.    fwrite($fh, $inf);
  229.    fclose($fh);*/
  230.  
  231.    $z = file_get_contents('fb.txt');
  232.    $u=json_decode ($z);
  233.  
  234. function _mks($s) {
  235.       return  iconv( 'utf-8','windows-1251', $s);
  236.    }
  237.  
  238.  
  239.    //echo '<pre>'.print_r($u).'</pre>';
  240.  
  241.  
  242. //-------------------------------------------------------
  243. $ff = <<<INF
  244.   <style type="text/css">
  245.  
  246.             .zzui_block {
  247.                width:330px;
  248.                background-color:white;
  249.                margin:10px 0 0 0;
  250.                font-family:'lucida grande', tahoma, verdana, arial, sans-serif;
  251.                font-size:80%;
  252.                padding: 5px;
  253.                height:300px;
  254.                overflow:hidden;
  255.             }
  256.  
  257.            
  258.  
  259.             .zzui_info {
  260.               font-size:90%;
  261.               border: solid 1px #aaa;
  262.               width: 290px;
  263.               padding:10px;
  264.               margin: 10px;
  265.               height:180px;
  266.               overflow:hidden;
  267.  
  268.             }
  269.  
  270.             .zzui_ref {
  271.                text-decoration: none;
  272.                color:inherit;
  273.             }
  274.  
  275.             .zzui_name {
  276.                font-size:100%;
  277.                color:#555;
  278.                margin-left:100px;
  279.             }
  280.            
  281.             .zzui_caption {
  282.                color:#777;
  283.                margin: 5px auto;
  284.                float:left;
  285.                width:100%;
  286.              
  287.             }
  288.             .zzui_desc {
  289.             }
  290.             .zzui_pic {
  291.                float:left;
  292.                margin-right:10px;
  293.             }
  294.          </style>
  295. INF;
  296. echo $ff;
  297.  
  298.  
  299.    foreach ($u->data as $inf) {
  300.       //------------------------
  301.       echo '<div class="zzui_block">';
  302.  
  303.       if (@$inf->message) {
  304.          $m = $inf->message;
  305.          $m = _mks($m);
  306.          $m = preg_replace('/(https?:\/\/[^\s]+(\s|$))/ims', '<a href="\1" target="_blank">\1</a>',$m);
  307.          echo "<div class=\"zzui_mes\">$m</div>";
  308.       }
  309.       //------------------------
  310.       if (@$inf->name) {
  311.          $link = $inf->link;
  312.          $name = @_mks($inf->name);
  313.          $caption = @_mks($inf->caption?$inf->caption:"");
  314.          $description = @_mks(($inf->description)?$inf->description:"");
  315.          
  316.          $picture = @($inf->picture)?($inf->picture):"";
  317.          
  318.  
  319.          $ss = <<<INF
  320.  
  321.          <a href="$link" class="zzui_ref" target="_blank">
  322.           <div class="zzui_info">
  323.              <div class="zzui_pic"><img src="$picture" /></div>
  324.              <h1 class="zzui_name">$name</h1>
  325.              <div class="zzui_caption">$caption</div>
  326.              <div class="zzui_desc">$description</div>  
  327.          </div>
  328.          </a>
  329.  
  330. INF;
  331.          echo $ss;    
  332.       } else {
  333.      
  334.       if (@$inf->picture) {
  335.          $m = $inf->picture;    
  336.          $link = $inf->link;
  337.  
  338.          $ss = <<<INF
  339.          <a href="$link" target="_blank">
  340.             <img src="$m"/>
  341.          </a>
  342. INF;
  343.         echo $ss;
  344.  
  345.       }}
  346.      
  347.      //-----------------------
  348.      echo '</div>';
  349.    }
  350.  
  351. ?>
  352.  
  353. //---------------------------------------------------------------------------------------------------
  354. //---------------------------------------------------------------------------------------------------
  355.  
  356. <?php
  357.  
  358.  
  359. //-------------------------------------
  360. function fill_db($db, $arr) {
  361.    
  362.    $sqls = array (sprintf('insert into fb_me
  363.                    (fb_id, fb_name, fb_first_name,
  364.                     fb_last_name, fb_link, fb_username,
  365.                     fb_birthday, fb_location_id,
  366.                     fb_location_name, fb_gender, fb_email)
  367.                  values(\'%s\',\'%s\',\'%s\',\'%s\',\'%s\',\'%s\',\'%s\',
  368.                   \'%s\',\'%s\',\'%s\',\'%s\') ',
  369.                      
  370.    sqlite_escape_string($arr["me"]["id"]),
  371.    sqlite_escape_string($arr["me"]["name"]),
  372.    sqlite_escape_string($arr["me"]["first_name"]),
  373.    sqlite_escape_string($arr["me"]["last_name"]),
  374.    sqlite_escape_string($arr["me"]["link"]),
  375.    sqlite_escape_string($arr["me"]["username"]),
  376.    sqlite_escape_string($arr["me"]["birthday"]),
  377.    sqlite_escape_string($arr["me"]["location"]["id"]),
  378.    sqlite_escape_string($arr["me"]["location"]["name"]),
  379.    sqlite_escape_string($arr["me"]["gender"]),
  380.    sqlite_escape_string($arr["me"]["email"])
  381.  
  382.                         )
  383.  
  384.  
  385.                           );
  386.  
  387.    for ($i=0; $i<count($sqls); $i++) {
  388.       if (!$db->query($sqls[$i]))
  389.          echo sqlite_error_string($db->lastError()).'<br/>';
  390.       else {
  391.  
  392.          $r_id = $db->lastInsertRowid();
  393.  
  394.          echo 'record inserted with rowid:'.$r_id;
  395.          }
  396.      
  397.    }
  398.  
  399.    
  400. };
  401. //-------------------------------------
  402. function cr_db() {
  403.  
  404.   $sqls = array (
  405.         ' DROP TABLE fb_me',
  406.  
  407.         ' CREATE TABLE  fb_me(
  408.          fb_id int,
  409.          fb_name varchar(100),
  410.          fb_first_name varchar(100),
  411.          fb_last_name varchar(100),
  412.          fb_link varchar(300),
  413.          fb_username varchar(100),
  414.          fb_birthday varchar(30),
  415.          fb_location_id int,
  416.          fb_location_name varchar(100),
  417.          fb_gender varchar(30),
  418.          fb_email varchar(100))');
  419.      
  420.  
  421.    if ($db = new SQLiteDatabase('testfbdb.dat')) {
  422.       //refl_check($db);
  423.  
  424.      
  425.       for ($i=0; $i<count($sqls); $i++) {
  426.          if (!$db->query($sqls[$i])) {
  427.             echo (sqlite_error_string($db->lastError())).'<br/>';
  428.          }
  429.       }
  430.      
  431.    } else {
  432.       throw new Exception('error opening database');
  433.    }
  434.  
  435.    return $db;
  436. }
  437. //-----------------------------------------
  438. function myHandler($e) {
  439.    echo '<br/><b>Uncaught exception: '.$e->getMessage().'</b><br/>';
  440. }
  441.  
  442. //-----------------------------------------
  443. function myErrorHandler($errno, $errstr, $errfile, $errline) {
  444.  
  445.    switch ($errno) {
  446.       case E_ERROR: echo '<br/>Error:<br/>'.$errstr."<br/>Line:$errline<br/>";
  447.                          break;
  448.  
  449.       case E_WARNING: echo '<br/>Warning:<br/>'.$errstr."<br/>Line:$errline<br/>";
  450.                            break;
  451.  
  452.       case E_NOTICE: echo '<br/>Notice:<br/>'.$errstr.'<br/>Line:'.$errline.'<br/>';
  453.                      break;
  454.      
  455.       default:  echo "<br/>errno:$errno<br/>errstr:$errstr<br/>errline:$errline<br/>";
  456.    }
  457.    return true;
  458. }
  459.  
  460. //-----------------------------------------
  461.  
  462. set_exception_handler('myHandler');
  463. $old_error_handler=set_error_handler('myErrorHandler');
  464.  
  465.  
  466. define('INFF',
  467. 'http://socium.unisample.com/logs/uinfo_kitsunesama.zinfo_11_10_2012_09_19_53_5293.txt');
  468.  
  469. //-------------------------------------
  470. function refl_check($obj) {
  471.  
  472.    $clname = get_class($obj);
  473.    echo $clname;
  474.    $mt = get_class_methods($clname);
  475.    //-------------------------
  476.    echo '<pre>';
  477.    var_dump($mt);
  478.    echo '</pre>';
  479.    //-------------------------
  480.    $cl_v = get_class_vars($clname);
  481.  
  482.    //-------------------------
  483.    echo '<pre>';
  484.    var_dump($cl_v);
  485.    echo '</pre>';
  486.  
  487.    echo str_repeat('-',40).'<br/>';
  488.    echo 'Reflections<br/>';
  489.    echo str_repeat('-',40).'<br/>';
  490.  
  491.    $rfl = new ReflectionClass($clname);
  492.  
  493.  
  494.    $mth = $rfl->getMethods();
  495.    echo '<pre>'.var_export($mth, true).'</pre>';
  496.  
  497.    $prp = $rfl->getProperties();
  498.    echo '<pre>'.var_export($prp, true).'</pre>';
  499.  
  500.    $cnt = $rfl->getConstants();
  501.    echo '<pre>'.var_export($cnt, true).'</pre>';
  502.    
  503.  
  504.    
  505.    
  506. }
  507.  
  508. //-------------------------------------
  509.  
  510.  
  511. $s = file_get_contents(INFF);
  512.  
  513. $arr = array();
  514. $arr= unserialize($s);
  515. //header('content-type: text/html; charset=utf-8');
  516. //echo '<pre>'.print_r($arr, true).'</pre>';
  517. //echo '<pre>'.var_export($arr, true).'</pre>';
  518.  
  519.    $db = cr_db();
  520.    fill_db($db, $arr);
  521.  
  522.  
  523.  
  524. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement