Facebook连接API,user_id始终为null我无法恢复我的个人资料

时间:2014-01-07 12:13:25

标签: php facebook-graph-api zend-framework facebook-php-sdk

这是我在stackoverflow中的第一篇文章,我希望我能为我的问题找到正确的答案,因为我到处都在研究找到解决方案,但没有任何帮助...所以我正在研究一个zend项目,在我的Facebookcontroller.php中,我将所有需要它的功能用于成功连接到我的网站并获取有关我的profil的信息。事实上,这是我第一次使用这个API,所以我选择使用PHP SDK。问题是我的$ uid中总是有一个大的“0”,所以我无法恢复我的个人资料,我无法将这些信息存储在我的数据库中...我希望很清楚,这是代码我的控制器。关于facebook应用程序,我在www.developer.facebook.com创建了一个,它工作正常,我可以进行身份​​验证,然后重定向到我的index.php。谢谢你的帮助。

<?php
/**
 * Zend Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category   Zend
 * @package    controller
 * @subpackage Facebook
 * @copyright  Copyright (c) 2005-2010 PHPSA . (http://www.phpsa.co.za)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 * @version    $Id: FacebookController.php 
 */
  class FacebookController extends Zend_Controller_Action
{

    public function indexAction(){

require_once 'API/facebook.php';
$appId = 'xxxxxxxxx';
$appSecret = 'xxxxxxxxxxxxxxxxxxxxxxx';

// Create your Application instance (replace this with your appId and secret).
$facebook = new Facebook( 
    array(
      'appId'  => $appId,
      'secret' => $appSecret,
    'fileUpload' => true,
    'cookie' => true

));
// Get User ID
$uid = $facebook->getUser();

if ($uid) 
{
    try {
        // Proceed knowing you have a logged in user who's authenticated.
        $user_profile = $facebook->api('/me');

    } catch (FacebookApiException $e) {
        error_log($e);
        $uid = null;
    }
}

// Login or logout url will be needed depending on current user state.
if ($uid) 
{
    $logoutUrl = $facebook->getLogoutUrl();
} 
    else 
    {
        $loginUrl = $facebook->getLoginUrl(
                            array(
                                'canvas' => 1,
                                'fbconnect' => 0,
                                'scope' =>'email,user_photos,user_birthday,offline_access',//these are the extended permissions you will need for your app, add/remove according to your requirement
                                'redirect_uri' => 'http://localhost:82/myProject/public/index.php',//this is the redirect uri where user will be redirected after allow,
                                ));                                       
    }

        if ($uid)
   {


    mysql_connect("localhost", "root", "");
    mysql_select_db("export"); 
    $result = mysql_query("INSERT INTO users(id, firstname, lastname, birthday, email, facebook,gender) VALUES('','$user_profile[first_name]','$user_profile[last_name]','$user_profile[birthday]', '$user_profile[email]','$user_profile[id]','$user_profile[gender]')"); 
    if (!$result)
    {
     die('Requête invalide : ' . mysql_error());
    }

  }
    else
    {
     //redirect user to loginUrl
     echo "<script>parent.location = '".$loginUrl."';</script>";
    }
}
}
?>

0 个答案:

没有答案