0

I'm using Janrain for my website users so that they can login into the website with any of their Social media account. Everything is working fine in terms of authorization, login and getting the user profile data but where it's wrong is when it comes to store their profile data in my local database. For e.g. user email, name, birthdate, photo, etc.

After user authorizes the app we get their profile data in the below format and after getting this data I want to store it into different variables so that it can be Inserted easily in the database.

$email = [email];//value should be joe@example.com

$name= [displayname];//value should be Pranav Bhat's

$photo= [photo];//value should be https://graph.facebook.com/1190480706/picture?type=large

$dob= [birthday];//value should be 1989-12-20

Code

<?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>

Output

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
)
colourtheweb
  • 727
  • 2
  • 13
  • 28
  • 1
    What's the _actual problem_ you're having? We don't write code for you for free; you need to at least have tried to solve the problem first. – Christian Apr 30 '13 at 14:08
  • @ChristianVarga Mate I have tried the different codes basically I want to take out the value from array and assign it to different variables. I'm not asking you for writing the code for me but just a suggestion regarding what should I do to make it work will be helpful. – colourtheweb Apr 30 '13 at 14:11
  • No worries, I can see you've edited the question with the code, that's more what we're looking for :). We need to see what you've tried before we can help, otherwise it really does look a request for someone to write code for you. – Christian Apr 30 '13 at 14:17

1 Answers1

1

It's just an associative array, so you can access the values like so (assuming that output is in a variable named $array)

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

etc.

Christian
  • 19,605
  • 3
  • 54
  • 70
  • cool cheers, also I have updated my question and have put down the code! – colourtheweb Apr 30 '13 at 14:17
  • @colourtheweb no worries, have you got the code for the database sorted or is that the next step? – Christian Apr 30 '13 at 14:19
  • database code isn't a big deal it will just be an insert query which I'll do using AJAX and than I will assign the logged in user to the session !! was just wanted to get the value separated :)..thanks for the help cheers – colourtheweb Apr 30 '13 at 14:21