Staggan Posted April 3, 2012 Share Posted April 3, 2012 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 ("martyn@staggan.com" , "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 ("test@test.com", "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 Quote Link to comment https://forums.phpfreaks.com/topic/260264-json-decode-problem/ Share on other sites More sharing options...
scootstah Posted April 3, 2012 Share Posted April 3, 2012 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 ) ) Quote Link to comment https://forums.phpfreaks.com/topic/260264-json-decode-problem/#findComment-1333977 Share on other sites More sharing options...
Staggan Posted April 3, 2012 Author Share Posted April 3, 2012 I tried a different test and simply took the print of the $reply var and assigned it to another var, then decoded that var and it all worked... so not sure what the hell is going on.... Quote Link to comment https://forums.phpfreaks.com/topic/260264-json-decode-problem/#findComment-1333978 Share on other sites More sharing options...
Staggan Posted April 3, 2012 Author Share Posted April 3, 2012 Sorry I can do what you are doing and get a correct result.. but it is NOT working in my code.... Quote Link to comment https://forums.phpfreaks.com/topic/260264-json-decode-problem/#findComment-1333982 Share on other sites More sharing options...
Staggan Posted April 3, 2012 Author Share Posted April 3, 2012 I just did a dump of the $jsonPacket var to find it is NULL So this line results in a NULL var: $jsonPacket = $json->decode($reply,true); and I have no idea why as $reply contains a valid JSON string Quote Link to comment https://forums.phpfreaks.com/topic/260264-json-decode-problem/#findComment-1333985 Share on other sites More sharing options...
Staggan Posted April 4, 2012 Author Share Posted April 4, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/260264-json-decode-problem/#findComment-1334368 Share on other sites More sharing options...
salathe Posted April 4, 2012 Share Posted April 4, 2012 You will likely want to modify your do_post_request() function to not include the headers, unless there is any particular reason why you need them. Quote Link to comment https://forums.phpfreaks.com/topic/260264-json-decode-problem/#findComment-1334449 Share on other sites More sharing options...
brucegregory Posted October 10, 2012 Share Posted October 10, 2012 (edited) That might work Edited October 10, 2012 by brucegregory Quote Link to comment https://forums.phpfreaks.com/topic/260264-json-decode-problem/#findComment-1384354 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.