API Tutorial... Decoding JSON

Post here is you are having problems with any of the tutorials.
Post Reply
User avatar
phpkid01
Posts: 9
Joined: Wed Jul 13, 2011 4:37 pm

API Tutorial... Decoding JSON

Post by phpkid01 »

hey guys...

i have a question... you know the errors parameters in the JSON well its encoded in another array...

i was wondering how you decode that array aswell... to show the independant error messages as im kinda stuck...
init.inc.php:
session_start();
$login = surfcms::validate_credentials($_POST['username'], $_POST['password'], $_POST['key']);
$session = $login['session'];
$username = $login['username'];
$errors = $login['errors'];
if($session==1){
	$_SESSION['site'] = "scms_" . $username;
}else{
	echo $errors;	
}
Cheers :)
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: API Tutorial... Decoding JSON

Post by jacek »

You can use [url=http://php.netjson_decode/json_decode]json_decode[/url], just pass it the encoded string and you will get the original array back.
Image
User avatar
phpkid01
Posts: 9
Joined: Wed Jul 13, 2011 4:37 pm

Re: API Tutorial... Decoding JSON

Post by phpkid01 »

so i should use:
session_start();
$login = surfcms::validate_credentials($_POST['username'], $_POST['password'], $_POST['key']);
$session = $login['session'];
$username = $login['username'];
$errors = json_decode($login['errors']);
if($session==1){
	$_SESSION['site'] = "scms_" . $username;
}else{
	foreach($errors as $error){
		echo $error;
	}	
}
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: API Tutorial... Decoding JSON

Post by jacek »

No, the function handles the decoding, you just use the result.

You should just be able to use $login['errors'] directly.
Image
User avatar
phpkid01
Posts: 9
Joined: Wed Jul 13, 2011 4:37 pm

Re: API Tutorial... Decoding JSON

Post by phpkid01 »

it only returns "Array" as the result... :/
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: API Tutorial... Decoding JSON

Post by jacek »

phpkid01 wrote:it only returns "Array" as the result... :/
you can't just echo an array. try doing a print_r($something) to see the format of the data you are getting.
Image
Post Reply