0

我要直奔主题了。我正在我的本地主机上使用 MAMP 构建一个原型 PHP 应用程序。

我想让最终用户尽可能轻松地进行注册转换。

因此,对于初学者来说,我使用 fb phpsdk 来获取所有用户图形数据以无摩擦地构建用户配置文件。我的一切都显示得很好。

但是,我正在努力让数据使用 PDO 成功存储。在我将其用于下面的示例之前,我成功地使用带有表单输入的 $_POST 方法使代码工作。

我只是不明白为什么没有 $_POST 方法它就不能工作。数据库设置得很好,就像我说我测试它并使用下面的 PDO 代码一样。

使用 PDO 尝试存储到数据库

require_once 'fbphpsdk/src/facebook.php'; // include the facebook php sdk
$facebook = new Facebook(array(
        'appId' => 'xxxx', // app id
        'secret' => 'xxxx')// app secret
);
$user = $facebook->getUser();
if ($user) { // check if current user is authenticated
    try {
        // Proceed knowing you have a logged in user who's authenticated.
        $user_profile = $facebook->api('/me');
    } catch (Exception $e) {}
}

$user = "xxxx";
$pwd = "xxxx";

$odb = new PDO("mysql:host=localhost;dbname=xxxx;", $user, $pwd);

$q = "INSERT INTO users(u_fullname, age, u_location, u_gender, u_bday, u_employer, u_job) VALUES(:u_fullname, :age, :u_location, :u_firstname, :u_gender, :u_bday, :u_employer, :u_job)";
$query = $odb->prepare($q);
$results = $query->execute(array(
        ":u_fullname" => $u_fullname,
        ":age" => $age,
        ":u_location" => $u_location,
        ":u_gender" => $u_gender,
        ":u_bday" => $u_bday,
        ":u_employer" => $u_employer,
        ":u_job" => $u_job));

fb 用户数据 (PHP)

$fbuid = $user_profile['id'];
$u_employer = $user_profile['work'][0]['employer']['name'];
$u_job = $user_profile['work'][0]['position']['name'];
$u_firstname = $user_profile['first_name'];
$u_fullname = $user_profile['name'];
$u_location = $user_profile['location']['name'];
$u_gender = ucfirst($user_profile['gender']);
$u_bday = $user_profile['birthday'];

//explode the date to get month, day and year
$u_bday = explode("/", $u_bday);

//get age from date or birthdate
$age = (date("md", date("U", mktime(0, 0, 0, $u_bday[0], $u_bday[1], $u_bday[2]))) > date("md") ? ((date("Y")-$u_bday[2])-1):(date("Y")-$u_bday[2]));?>

有任何想法吗?

4

2 回答 2

1

你的查询是错误的,替换

:u_firstname你的查询中有一个额外的

$q = "INSERT INTO users(u_fullname, age, u_location, u_gender, u_bday, u_employer, u_job) VALUES(:u_fullname, :age, :u_location, :u_firstname, :u_gender, :u_bday, :u_employer, :u_job)";

$q = "INSERT INTO users(u_fullname, age, u_location, u_gender, u_bday, u_employer, u_job) VALUES(:u_fullname, :age, :u_location, :u_gender, :u_bday, :u_employer, :u_job)";

于 2013-10-12T06:45:54.910 回答
0

查看源代码,该库似乎POST默认使用。如果要使用GET请求,则必须method在调用api()方法时提供参数。我的 PHP 越来越生疏了,但我会看看我是否能想出你需要使用的正确语法......

编辑:

看起来你想要类似的东西:

$user_profile = $facebook->api(
    '/me', 
    array('method' => 'GET')
);

这是未经测试的(我目前没有 PHP 的测试环境),但我从项目的 Github 页面中的测试中提取了它:https ://github.com/facebook/facebook-php-sdk/blob/主/测试/测试.php

于 2013-10-12T07:21:19.253 回答