Código:
<?php /*! * QAScript v1.2.0 * http://ifsoft.com.ua * [email protected] * * Copyright (C) 2012-2014 Demyanchuk Dmitriy (Russian Dimon) */ Class facebook extends db_connect { static protected $oAppId = ""; //APP ID static protected $oAppSecret = ""; //APP SECRET static protected $oRedirectUri = "http://demo.ifsoft.com.ua/login/?action=facebook"; static protected $oCode = NULL; static protected $oInstance = NULL; public function __construct($dbo=NULL) { parent::__construct($dbo); } protected function getCode() { if(isset($_REQUEST["code"])) { self::$oCode = $_REQUEST["code"]; } } public function getLink() { $this->getCode(); if(!self::$oCode) { $_SESSION['state'] = md5(uniqid(rand(), TRUE)); $oDialogUrl = "http://www.facebook.com/dialog/oauth?client_id=" . self::$oAppId . "&redirect_uri=" . urlencode(self::$oRedirectUri) . "&state=" . $_SESSION["state"]; return $oDialogUrl; } } public function logIn() { if (!isset($_REQUEST['error'])) { if(isset($_REQUEST['state']) and isset($_SESSION['state'])) { if($_REQUEST['state'] == $_SESSION['state']) { $this->getCode(); $tokenUrl = "https://graph.facebook.com/oauth/access_token?client_id=" . self::$oAppId . "&redirect_uri=" . urlencode(self::$oRedirectUri) . "&client_secret=" . self::$oAppSecret . "&code=" . self::$oCode; $oResponse = @file_get_contents($tokenUrl); $params = NULL; parse_str($oResponse, $params); $graphUrl = "https://graph.facebook.com/me?access_token=" . $params['access_token']; $oUser = json_decode(@file_get_contents($graphUrl)); $app = new app($this->db); $app->fbLogin($oUser->id, $oUser->first_name, $oUser->last_name, $oUser->username); unset($app); header("Location: /login/?status=success_fb_login"); } } else { // echo 'Error. $_REQUEST[\'state\'] or $_SESSION[\'state\'] is not available'; header("Location: /login/?status=error_fb_login"); } } else { header("Location: /login/?status=error_fb_login"); } } } ?>