Jump to content

JSON Decode problem


Staggan

Recommended Posts

I am having a nightmare sending / recieving some JSON.....

 

I have a test page which creates a JSON packet and posts it to another page. This page then processes it and returns a response... the data is in the post, but after decoding it the data seems to be lost....

 

Here is the code for the sending page:

 

 



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<?php

require_once ("inc/jsonwrapper/JSON/JSON.php");

$RequestID ='121'; //		:ID to which this request relates to allow site to keep track of things


$Method = 'login'; //:Which method the call is directed at
$referer 	= 'abc'; 	//	:Name of referring network
$affiliateid  =2468; 	//	:ID of referring site in affiliate network if any
$user_Country  ='gb'; 	//	:Users two letter country code
$UserID 	= '12345'; 	//	:Numeric  ID to identify players in game database belonging to network or portal 
$GameuserID = '1' ; // UserID in game database

$params = array ( 'referer' => $referer , 'affiliateid' => $affiliateid , 'userCountry' => $user_Country , 'UserID' => $UserID , 'GameuserID' => $GameuserID);


$response = array ( 'jsonrpc'=>'2.0', 'id'=> $RequestID , 'Method' => $Method , 'params' => $params) ;

$data = json_encode($response);

// URL of web api page

$url =  'http://xxx.com/webapi.php';

$reply = do_post_request($url, $data);

// setup new JSON service
$json = new Services_JSON();

$jsonPacket = $json->decode($reply,true); 

//THIS IS WHERE I PRINT THE RESPONSE 
echo print_r ($jsonPacket,1);



function do_post_request($url, $data, $optional_headers = null)
{
     $params = array('http' => array(
                  'method' => 'POST', 
      'header'=>"Content-Type: text/xml; charset=utf-8",
                  'content' => $data
               ));
     
  
  if ($optional_headers !== null) {
        $params['http']['header'] = $optional_headers;
     }
     $ctx = stream_context_create($params);
     $fp = @fopen($url, 'rb', false, $ctx);
     if (!$fp) {
        throw new Exception("Problem with $url, $php_errormsg");
     }
     $response = @stream_get_contents($fp);
     if ($response === false) {
        throw new Exception("Problem reading data from $url, $php_errormsg");
     }
     return $response;
}
?>

 

 

And the replying page

 


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>


<?

//setup database connection
require_once('globals.php');

//setup JSON
require_once ("inc/jsonwrapper/JSON/JSON.php");
$secret_Key = '12345';//SECRET KEY
$app_Id = '6789';//APP ID
$token = '';


// setup new JSON service
$json = new Services_JSON();

// accept incoming POST data
$input = $GLOBALS['HTTP_RAW_POST_DATA'];


//hashes
$hash = $_GET['authHash'];
$sHash  = strtolower(md5($input . $secret_Key));

$hash = 0;
$sHash  = 0;

if (strcmp($sHash, $hash) == 0) {


//check if incoming is JSON as we MIGHT use XML later too
$isJSON=  substr_count($input,"json");

if ($isJSON > 0){

processJSON ($input);
}


global $hash, $sHash;


}



//JSON FUNCTION TO PARSE JSON 
function processJSON($input){


global $json;	
$jsonPacket = $json->decode($input,true); 
$json_RequestId = $jsonPacket->id;



//check method and go to correct function
switch ($jsonPacket->Method){

case 'Game.login':
		game_login ($jsonPacket, $json_RequestId) ;
		break;


}
}





//JSON FUNCTIONS


function game_login($jsonPacket, $json_RequestId){


	$aid = ($jsonPacket->params->affiliateid);

	$country_code = ($jsonPacket->params->userCountry);

	$GameuserID = ($jsonPacket->params->GameuserID);

	$UserID = ($jsonPacket->params->UserID);
	//mail ("[email protected]" , "SNSUserID" , $UserID  );

	$result_userIDcheck = mysql_query("SELECT snsUserid FROM player where player_id = $GameuserID;");
	$result_UserID = mysql_result ($result_userIDcheck, 0);


	if ($UserID == $result_UserID){
	$result = 'OK';
	$id = $json_RequestId;

	//create token
	global $secret_Key ;
	$token = hash ('md5', $secret_Key.$aid.$GameuserID);

	$logindetails = array ( 'token=' => $token , 'UID'=> $GameuserID , 'snsUID' => $UserID , 'aid' => $aid , 'name' => $name);

	$params= array ('result'=>$result, 'logindetails'=> $logindetails, 'GameuserID'=>$GameuserID);
	$response = array ( 'jsonrpc'=>'2.0', 'id'=> $id , 'result' => $params) ;
	echo json_encode($response);
	}

	else mail ("[email protected]", "login", 'false');
}



?>

 

If I print the $reply var I see this:

 

{"jsonrpc":"2.0","id":"121","result":{"result":"OK","logindetails":{"token=":"68c49918c353a3e5d86d165da1cb2f72","UID":"1","snsUID":"12345","aid":"6789","name":null},"GameuserID":"1"}}

 

But trying to decode to an array leaves me with nothing....

 

Any help would be greatly appreciated

 

 

Link to comment
https://forums.phpfreaks.com/topic/260264-json-decode-problem/
Share on other sites

It works for me.

print_r(json_decode('{"jsonrpc":"2.0","id":"121","result":{"result":"OK","logindetails":{"token=":"68c49918c353a3e5d86d165da1cb2f72","UID":"1","snsUID":"12345","aid":"6789","name":null},"GameuserID":"1"}}'));

stdClass Object
(
    [jsonrpc] => 2.0
    [id] => 121
    [result] => stdClass Object
        (
            [result] => OK
            [logindetails] => stdClass Object
                (
                    [token=] => 68c49918c353a3e5d86d165da1cb2f72
                    [uID] => 1
                    [snsUID] => 12345
                    [aid] => 6789
                    [name] => 
                )

            [GameuserID] => 1
        )

)

Link to comment
https://forums.phpfreaks.com/topic/260264-json-decode-problem/#findComment-1333977
Share on other sites

OK, so, it appears my JSON response contains HTTP headers which are what is causing the problem when I try to decode them....

 

I use

 


echo json_encode($response);

 

To send my response, but it includes the headers... so when I use json_decode it breaks

 

Any ideas?

 

Thanks

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/260264-json-decode-problem/#findComment-1334368
Share on other sites

  • 6 months later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.