1

I am executing a curl request and get a response which returns a json response. Below is the code after the response is sent back.

Response: "Zeros Replaced real token"

{"success":true,"result":{"token":"000000000","serverTime":1471365111,"expireTime":1471365411}}1

Code Used (For Testing) and accessing property: $json = json_decode($result); print_r($json); // Prints the Json Response

$firsttry = $json->result['token']; //Access Property results in error :Trying to get property of non-object
$secondtry = $json['token']; 

echo $firsttry.'<br>';//Code can't continue because of error from $firsttry.
print_r( $secondtry.'<br>');//Nothing Prints at all

I did notice a weird anomaly where it prints a 1 at the end, where as if i do

json_encode($json);

The return response replaces the one at the end of the string with a "true" Could the "1 or true" at the end be throwing of the json decode?

Maybe I am missing something simple?

As Requested full test code

$url = "https://website.com/restapi.php";
//username of the user who is to logged in. 
$userName="adminuser"; //not real user

$fields_string; //global var


$fields = array( //array will have more in the future
'username' => urlencode($userName)
 );

//url-ify the data for the POST
foreach($fields as $key=>$value) { global $fields_string;
$fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,         $url.'?'.$fields_string.'operation=getchallenge');
curl_setopt($ch,CURLOPT_POST, count($fields));

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);
DEVPROCB
  • 483
  • 2
  • 5
  • 19
  • *"I did notice a weird anomaly where it prints a 1 at the end"* It's not coming from `json_encode`, it's coming from something *after* `json_encode`. – T.J. Crowder Aug 16 '16 at 17:28
  • The `1` at the end of the JSONString makes that an invalid JSONString – RiggsFolly Aug 16 '16 at 17:28
  • so I would assume using something like rtim at the end of the response would fix that? Let me try that. – DEVPROCB Aug 16 '16 at 17:29
  • You had better show us the code where you receive the curl response as that is likely as not where you are adding a `1` – RiggsFolly Aug 16 '16 at 17:29
  • Dont fix the symptom, **fix the cause** – RiggsFolly Aug 16 '16 at 17:29
  • added the curl request as requested. even when trimming the result I am still running into a trying to get property of non-object. – DEVPROCB Aug 16 '16 at 17:59
  • *"Maybe I am missing something simple?"* -- you missed a lot of things. [`json_decode()`](http://php.net/manual/en/function.json-decode.php) accepts `TRUE` as its second argument to return arrays instead of objects. The [`global`](http://php.net/manual/en/language.variables.scope.php) keyword, besides being bad practice, doesn't have any effect outside of a function. The function [`http_build_query()`](http://php.net/manual/en/function.http-build-query.php) "url-ifies" the data in a single line of code and does it correctly. – axiac Aug 17 '16 at 19:17
  • @axiac this was just sample code to get it to work. It was so that I can spit out strings when I needed to. Once I get a code to work, I start cleaning it up to be much tighter. The issue came down to not having : curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); in the curl portion. When this was added, the rest of the script returned correctly and I was able to access the json properties correctly. Also I forgot to include the rest of the code below curl_close which included the json_decod() variable which is where the $json variable came from. They had only requested the curl code. – DEVPROCB Aug 17 '16 at 19:20

3 Answers3

2

json_decode(), by default makes child objects into stdClass objects rather than arrays unless they are explicitly arrays.

Try something like:

$firsttry = $json->result->token;
TheGentleman
  • 2,324
  • 13
  • 17
  • It does not do it by default it does it because it see's a `{` rather than a `[` – RiggsFolly Aug 16 '16 at 17:31
  • Hence the word explicitly (`[` is an explicit array in json). PHP allows you to pass a parameter to `json_decode()` that will force it to use associative arrays rather than `stdClass` objects. – TheGentleman Aug 16 '16 at 17:32
1

The var_dump shows you the data type. Since result itself is an object, access its token with -> rather than []

$response = '{"success":true...}'
$json = json_decode($response); //var_dumping this will show you it's an object
echo $json->result->token; // 000000000
BeetleJuice
  • 39,516
  • 19
  • 105
  • 165
  • when running var dump I am actually getting a NULL. when running vardump on the result without json decode I get a string(0) "" – DEVPROCB Aug 16 '16 at 18:02
0

I figured out the issue. In the Curl Options I did not have

curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 

Once i put this in @GentelmanMax solution worked for me, but the issue was in the curl response responding directly, where as the return transfer sends back a string that php can work with, which then allowed json_decode()to function as is should. I knew it was something simple.

DEVPROCB
  • 483
  • 2
  • 5
  • 19