2013-04-30 67 views
0

我爲我的網站用戶使用Janrain,以便他們可以使用他們的任何社交媒體帳戶登錄該網站。在授權,登錄和獲取用戶配置文件數據方面,一切工作都很好,但在何處將其配置文件數據存儲在本地數據庫中時出錯。對於例如用戶電子郵件,姓名,出生日期,照片等我應該如何通過Janrain在不同的變量中存儲數據?

用戶授權應用程序後,我們得到他們的個人資料數據在下面的格式,並獲得這些數據後,我想存儲到不同的變量,以便它可以很容易地插入數據庫。

$電子郵件 = [郵件]; //值應爲[email protected]

$命名 = [顯示名稱]; //值應Pranav銖的

$照片 = [照片]; //值應爲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] => [email protected] 
      [preferredUsername] => PranavBhat's 
      [displayName] => Pranav Bhat's 
      [name] => Array 
       (
        [formatted] => Pranav Bhat's 
        [givenName] => Pranav 
        [familyName] => Bhat's 
       ) 

      [email] => [email protected] 
      [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 
) 
+1

你有什麼實際問題?我們不會爲您免費編寫代碼;你至少需要先嚐試解決問題。 – 2013-04-30 14:08:53

+0

@ChristianVarga Mate我已經嘗試了不同的代碼,基本上我想從數組中取出值並將其分配給不同的變量。我並沒有要求你爲我編寫代碼,而只是建議我應該怎麼做才能使它工作起來會有幫助。 – colourtheweb 2013-04-30 14:11:02

+0

不用擔心,我可以看到你已經用代碼編輯了這個問題,這更多的是我們正在尋找的:)。在我們能夠幫助之前,我們需要看看你已經嘗試了什麼,否則它確實會尋找某人爲你寫代碼的請求。 – 2013-04-30 14:17:34

回答

1

這只是一個關聯數組,這樣你就可以訪問值,像這樣(假設輸出是一個名爲$array一個變量)

$email = $array['profile']['email']; 
$name = $array['profile']['displayName']; 

+0

很酷的歡呼聲,我也更新了我的問題,並已放下代碼! – colourtheweb 2013-04-30 14:17:09

+0

@colourtheweb不用擔心,你是否已經對數據庫的代碼進行了排序或者是下一步? – 2013-04-30 14:19:10

+0

數據庫代碼不是什麼大事,它只是一個插入查詢,我將使用AJAX,而不是將分配登錄用戶加入會話!只是想得到價值分離:) ..感謝您的幫助歡​​呼聲 – colourtheweb 2013-04-30 14:21:50

相關問題