我应该如何将通过 Janrain 传入数组的数据存储在不同的变量中?
Posted
技术标签:
【中文标题】我应该如何将通过 Janrain 传入数组的数据存储在不同的变量中?【英文标题】:how should I store data coming in array through Janrain in different variables? 【发布时间】:2013-04-30 14:05:49 【问题描述】:我正在为我的网站用户使用 Janrain,以便他们可以使用任何社交媒体帐户登录网站。在授权、登录和获取用户个人资料数据方面一切正常,但错误之处在于将他们的个人资料数据存储在我的本地数据库中。例如用户邮箱、姓名、生日、照片等
在用户授权应用程序后,我们以以下格式获取他们的个人资料数据,在获得这些数据后,我想将其存储到不同的变量中,以便可以轻松地将其插入数据库中。
$email = [email];//值应该是 joe@example.com
$name= [displayname];//值应该是Pranav Bhat's
$photo= [照片];//值应该是https://graph.facebook.com/1190480706/picture?type=large
$dob= [生日];//值应该是 1989-12-20
代码
<?php
/**
* Copyright 2011
* Janrain Inc.
* All rights reserved.
*/
/**
* Below is a very simple PHP 5 script that
* implements an Engage token URL to collect
* and output the results from auth_info.
* The code below assumes you have the
* CURL HTTP fetching library with SSL and
* PHP JSON support.
*/
ob_start();
require_once('../library/engage.auth.lib.php');
$debug_array = array('Debug out:');
/**
* For a production script it would be better
* to include (require_once) the apiKey in
* from a file outside the web root to
* enhance security.
*
* Set your API key (secret) in this file.
* The varable is $api_key
*/
require_once('engage-conf.php');
$token = $_POST['token'];
$format = ENGAGE_FORMAT_JSON;
$extended = $auth_info_extended;
$result = engage_auth_info($api_key, $token, $format, $extended);
if ($result === false)
$errors = engage_get_errors();
foreach ($errors as $error=>$label)
$debug_array[] = 'Error: '.$error;
else
/**
* On a successful authentication store
* the auth_info data in the variable
* $auth_info_array
*/
$array_out = true;
$auth_info_array = engage_parse_result($result, $format, $array_out);
//Put a printed copy in the debug.
$debug_array[] = print_r($auth_info_array, true);
/**
* This is the point to add code to do something with the Engage data.
*/
$errors = engage_get_errors(ENGAGE_ELABEL_ERROR);
foreach ($errors as $error=>$label)
$error_array[] = 'Error: '.$error;
/*
* Uncomment lines below to get SDK level
* debug data. Caution: This could result in
* revealing the api_key.
*/
//$debugs = engage_get_errors(ENGAGE_ELABEL_DEBUG);
//foreach ($debugs as $debug=>$label)
// $debug_array[] = 'Debug: '.$debug;
//
$the_buffer = ob_get_contents();
if (!empty($the_buffer))
$debug_array[] = 'Buffer: '.$the_buffer;
/* The variable (string) $the_debug will contain debug data. */
$the_debug = implode("\n", $debug_array);
$the_error = implode("\n", $error_array);
ob_end_clean();
?>
<html>
<head>
<title>Janrain Engage token URL example</title>
</head>
<body>
<pre>
<?php echo $the_error; ?>
<?php echo $the_debug; ?>
</pre>
</body>
</html>
输出
Array
(
[stat] => ok
[profile] => Array
(
[providerName] => Facebook
[identifier] => http://www.facebook.com/profile.php?id=1190480706
[verifiedEmail] => joe@example.com
[preferredUsername] => PranavBhat's
[displayName] => Pranav Bhat's
[name] => Array
(
[formatted] => Pranav Bhat's
[givenName] => Pranav
[familyName] => Bhat's
)
[email] => joe@example.com
[url] => http://www.facebook.com/bhats1989
[photo] => https://graph.facebook.com/1190480706/picture?type=large
[utcOffset] => 01:00
[address] => Array
(
[formatted] => London, United Kingdom
[type] => currentLocation
)
[birthday] => 1989-12-20
[gender] => male
[providerSpecifier] => facebook
)
[limited_data] => false
)
【问题讨论】:
您遇到的实际问题是什么?我们不会免费为您编写代码;你至少需要先尝试解决问题。 @ChristianVarga Mate 我尝试了不同的代码,基本上我想从数组中取出值并将其分配给不同的变量。我不是要求你为我编写代码,只是关于我应该怎么做才能使它工作的建议会有所帮助。 不用担心,我可以看到您已经用代码编辑了问题,这就是我们要寻找的更多内容:)。在我们提供帮助之前,我们需要先了解您的尝试,否则它确实看起来像是在请求某人为您编写代码。 【参考方案1】:它只是一个关联数组,因此您可以像这样访问值(假设输出位于名为 $array
的变量中)
$email = $array['profile']['email'];
$name = $array['profile']['displayName'];
等等
【讨论】:
很酷的欢呼声,我也更新了我的问题并记下了代码! @colourtheweb 不用担心,您是否已将数据库的代码排序或下一步? 数据库代码没什么大不了的,它只是一个插入查询,我将使用 AJAX 执行此操作,然后将登录用户分配给会话!只是想将值分开:)..感谢您的帮助欢呼以上是关于我应该如何将通过 Janrain 传入数组的数据存储在不同的变量中?的主要内容,如果未能解决你的问题,请参考以下文章