Ver Mensaje Individual
  #6 (permalink)  
Antiguo 13/10/2013, 08:47
Farrell
 
Fecha de Ingreso: septiembre-2013
Mensajes: 18
Antigüedad: 11 años, 2 meses
Puntos: 1
Respuesta: Inicio de session y registro facebook connet

jajajja mas wepon! aqui les dejo el code!

Código PHP:
Ver original
  1. <?php
  2.  
  3.  
  4. if (!empty($_SESSION)) {
  5.     header("Location: index.php");
  6. }
  7. mysql_connect('mihost', 'user', 'pass');
  8.  
  9. # require library
  10. require("facebook.php");
  11.  
  12. # Creating the facebook object
  13. $facebook = new Facebook(array(
  14.             'appId' => 'ID',
  15.             'secret' => 'secretid',
  16.             'cookie' => true
  17.         ));
  18.  
  19. # check active session
  20. $session = $facebook->getSession();
  21.  
  22.  
  23. if (!empty($session)) {
  24.     # session active, get user id (getUser()) and user info (api->('/me'))
  25.    try {
  26.         $uid = $facebook->getUser();
  27.         $fb_access_token=$session['access_token'];
  28.         $url = $facebook->getLoginUrl(array(
  29.             'req_perms' => 'email,status_update,publish_stream'
  30.                 ));
  31.         $user = $facebook->api('/me');
  32.         $photolink = 'http://graph.facebook.com/'.$user['id'].'/picture?type=square';
  33.         $param = array(
  34.             'method' => 'users.getInfo',
  35.             'uids' => uid,
  36.             'fields' => 'pic_big'
  37.         );
  38.         $users_getinfo = $facebook->api($param);
  39.     } catch (Exception $e) {
  40.  
  41.     }
  42.  
  43.     if (!empty($user)) {
  44.         # active session, check if already registered the user
  45.        $query = mysql_query("SELECT * FROM users WHERE oauth_provider = 'facebook' AND oauth_uid = " . $user['id']);
  46.         $result = mysql_fetch_array($query);
  47.  
  48.  
  49.         # If not, add it to the database
  50.        if (empty($result)) {
  51.             $query = mysql_query("INSERT INTO users (oauth_uid, oauth_provider, username, first_name, last_name, email, pic_square) VALUES ('facebook', {$user['id']}, '{$user['name']}', '{$user['first_name']}', '{$user['last_name']}','{$user['email']}', '".$photolink."')");
  52.             $query = mysql_query("SELECT * FROM users WHERE id = " . mysql_insert_id());
  53.             $result = mysql_fetch_array($query);
  54.         }
  55.  
  56.  
  57.         // variables in the session
  58.         $_SESSION['id'] = $result['id'];
  59.         $_SESSION['oauth_uid'] = $result['oauth_uid'];
  60.         $_SESSION['oauth_provider'] = $result['oauth_provider'];
  61.         $_SESSION['username'] = $result['username'];
  62.     } else {
  63.         # if error, kill the script
  64.        die("There was an error.");
  65.     }
  66.  
  67.     $api_call = array(
  68.         'method' => 'users.hasAppPermission',
  69.         'uid' => $uid,
  70.         'ext_perm' => 'publish_stream'
  71.     );
  72.     $can_post = $facebook->api($api_call);
  73.     if ($can_post) {
  74.         # post it!
  75.        $facebook->api('/' . $uid . '/feed', 'post', array(
  76.             'message' => 'Participate in Camry Slogan Contest and Win great prizes!',
  77.             'name' => 'Camry Slogan Contest',
  78.             'description' => 'Participate in Camry Slogan Contest and Win iPhone, iPad, Samsung Galaxy, Canono Digital Camera, Sony Play Station, Blackberry Bold and more prizes.',
  79.             'caption' => 'WIN prizes by suggesting us a Slogan for our new Toyota Camry!',
  80.             'picture' => 'http://www.masudonline.net/camry/headar.jpg',
  81.             'link' => 'http://apps.facebook.com/camryslgn/'
  82.         ));
  83.         echo 'Posted!';
  84.     } else {
  85.         die('Permissions required!');
  86.     }
  87. } else {
  88.     # no active session, generate one
  89.    $login_url = $facebook->getLoginUrl();
  90.     header("Location: " . $login_url);
  91. }
  92.  
  93. ?>