Jump to content

Recommended Posts

Hi all

 

I am trying to implent the janrain social login system into my website

 

I am new to JSON and so am not sure of what I am doing is correct.

 

Basically the user logs in and is returned to my token.php

 

The code I have here is:

 

<?php

include('scripts/include.php');
ob_start();
/*
Below is a very simple and verbose PHP 5 script that implements the Engage token URL processing and some popular Pro/Enterprise examples.
The code below assumes you have the CURL HTTP fetching library with SSL.  
*/

//For a production script it would be better to include the apiKey in from a file outside the web root to enhance security.
$rpx_api_key = 'mykeyishere';

/*
Set this to true if your application is Pro or Enterprise.
Set this to false if your application is Basic or Plus.
*/
$engage_pro = false;

/* STEP 1: Extract token POST parameter */
$token = $_POST['token'];

//Some output to help debugging
echo "SERVER VARIABLES:\n";
var_dump($_SERVER);
echo "HTTP POST ARRAY:\n";
var_dump($_POST);

if(strlen($token) == 40) {//test the length of the token; it should be 40 characters

  /* STEP 2: Use the token to make the auth_info API call */
  $post_data = array('token'  => $token,
                     'apiKey' => $rpx_api_key,
                     'format' => 'json',
                     'extended' => 'true'); //Extended is not available to Basic.

  $curl = curl_init();
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($curl, CURLOPT_URL, 'https://rpxnow.com/api/v2/auth_info');
  curl_setopt($curl, CURLOPT_POST, true);
  curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
  curl_setopt($curl, CURLOPT_HEADER, false);
  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($curl, CURLOPT_FAILONERROR, true);
  $result = curl_exec($curl);
  if ($result == false){
    echo "\n".'Curl error: ' . curl_error($curl);
    echo "\n".'HTTP code: ' . curl_errno($curl);
    echo "\n"; var_dump($post_data);
  }
  curl_close($curl);


  /* STEP 3: Parse the JSON auth_info response */
  $auth_info = json_decode($result, true);

  if ($auth_info['stat'] == 'ok') {
    echo "\n auth_info:";
    echo "\n"; var_dump($auth_info);

    /* Pro API examples */
    /* Basic and Plus please skip down to Step 4 */
    if ($engage_pro === true){

      /* Extract the needed variables from the response */
      $profile = $auth_info['profile'];
      $identifier = $profile['identifier'];
      $provider = $profile['providerName'];

      /* Example of the get_contacts API */
      //Only run if the provider is one supported for get_contacts
      if ($provider == 'Google' || $provider == 'Twitter' || $provider == 'Facebook' || $provider == 'Yahoo!'){
        $post_data = array(
          'apiKey' => $rpx_api_key,
          'identifier' => $identifier
        );
        $url = 'https://rpxnow.com/api/v2/get_contacts';
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_FAILONERROR, true);
        $result = curl_exec($curl);
        if ($result == false){
          echo "\n".'URL:'.$url;
          echo "\n".'Curl error: ' . curl_error($curl);
          echo "\n".'HTTP code: ' . curl_errno($curl);
          echo "\n"; var_dump($post_data);
        }
        $get_contacts = json_decode($result);
        curl_close($curl);
        echo "\n get_contacts:";
        echo "\n"; var_dump($get_contacts);
      }

}/*


    /* STEP 4: Use the identifier as the unique key to sign the user into your system.
       This will depend on your website implementation, and you should add your own
       code here. The user profile is in $auth_info.
    */

$oid = $auth_info->identifier;
$add = mysql_query('INSERT INTO user (oid) VALUES ($oid)');
if(!$add)
{
	echo mysql_error();
}

    } else {
      // Gracefully handle auth_info error.  Hook this into your native error handling system.
      echo "\n".'An error occured: ' . $auth_info['err']['msg']."\n";
      var_dump($auth_info);
      echo "\n";
      var_dump($result);
    }
}else{
  // Gracefully handle the missing or malformed token.  Hook this into your native error handling system.
  echo 'Authentication canceled.';
}
$debug_out = ob_get_contents();
ob_end_clean();
?>

 

however the problem I am having is that nothing is entered into the db and no error is reported, view source is just blank?

 

Does anyone have any experience in this?

 

Cheers

 

 

Link to comment
https://forums.phpfreaks.com/topic/242148-help-with-getting-json-to-php/
Share on other sites

Sorry I am not sure I understand.

In changing


$oid = $auth_info->identifier;
$add = mysql_query('INSERT INTO user (oid) VALUES ($oid)');
if(!$add)
{
	echo mysql_error();
}

 

to

 

$oid = $auth_info['identifier'];
$add = mysql_query('INSERT INTO user (oid) VALUES ($oid)');
if(!$add)
{
	echo mysql_error();
}

 

I get this as a view source:

 


<html> 
  <head> 
    <script type="text/javascript"> 
      function ol() {
        if (document.forms[1].visited.value) {
          window.history.go(-1);
        } else {
          document.forms[1].visited.value = 'yes';
          //if (window == window.top) {
          document.forms[0].submit();
          //}
        }
      }
    </script> 
  </head> 
  <body onload="ol();"> 
    <form action="http://50dollargigs.com/token.php" method="POST"> 
      <input id="token" name="token" type="hidden" value="015d57a04292ea351166526a1c614e220ceb14a1" /> 
      <noscript> 
        <input type="submit" value="Continue">
      </noscript> 
    </form> 
    <form> 
      <input type="hidden" name="visited"/> 
    </form> 
  </body> 
</html> 

 

Just dont understand what it means!

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.