Jump to content

Recommended Posts

How to get a state return on a request?

Like: http://domain.com/example.php?name=Bob
Return: 'true'

or like: http://domain.com/example.php?name=Alex
Return: 'false'

Based on the online state

from this JSON:

127.0.0.1 - 2022-10-01 13:13:20 - INFO - [
{
	"config_group_id":2,
	"device_count":2,
	"devices":[
		"11:22:00:00:00:01",
		"11:22:00:00:00:02"
	],
	"name":"Bob",
	"pause":false,
	"profile_id":2,
	"timespent":{
		"has_quota":false,
		"quota":0,
		"total_spent":{
			"normal":91,
			"reward":0
		}
	},
	"online_device_count":1,
	"online":true
},
{
	"config_group_id":3,
	"device_count":2,
	"devices":[
		"11:22:00:00:00:03",
		"11:22:00:00:00:04"
	],
	"name":"Alex",
	"pause":false,
	"profile_id":3,
	"timespent":{
		"has_quota":false,
		"quota":0,
		"total_spent":{
			"normal":0,
			"reward":0
		}
	},
	"online_device_count":0,
	"online":false
},
{
	"config_group_id":9,
	"device_count":3,
	"devices":[
		"11:22:00:00:00:05",
		"11:22:00:00:00:06",
		"11:22:00:00:00:07"
	],
	"name":"$lan$",
	"pause":false,
	"profile_id":4,
	"timespent":{
		"has_quota":false,
		"quota":0,
		"total_spent":{
			"normal":638,
			"reward":0
		}
	},
	"online_device_count":2,
	"online":true
}
]

 

Link to comment
https://forums.phpfreaks.com/topic/315388-return-status-on-request/
Share on other sites

OK - I did remove the [] but now I've added them to the json variable and this is the output I have to show:

Step 1. A print_r of my json variable is:
 
[
	{
	"config_group_id":2,
	"device_count":2,
	"devices":
		[
			"11:22:00:00:00:01",
			"11:22:00:00:00:02"
		],
	"name":"Bob",
	"pause":false,
	"profile_id":2,
	"timespent":
		{
			"has_quota":false,
			"quota":0,
			"total_spent":
			{
				"normal":91,
				"reward":0
			}
		},
	"online_device_count":1,
	"online":true
	},
	{
	"config_group_id":3,
	"device_count":2,
	"devices":
		[
			"11:22:00:00:00:03",
			"11:22:00:00:00:04"
		],
	"name":"Alex",
	"pause":false,
	"profile_id":3,
	"timespent":
		{
			"has_quota":false,
			"quota":0,
			"total_spent":
			{
				"normal":0,
				"reward":0
			}
		},
	"online_device_count":0,
	"online":false
	},
	{
	"config_group_id":9,
	"device_count":3,
	"devices":
		[
			"11:22:00:00:00:05",
			"11:22:00:00:00:06",
			"11:22:00:00:00:07"
		],
	"name":"$lan$",
	"pause":false,
	"profile_id":4,
	"timespent":
	{
		"has_quota":false,
		"quota":0,
		"total_spent":
		{
			"normal":638,
			"reward":0
		}
	},
	{
	"online_device_count":2,
	"online":true
	}
	}
]

*********************
Step 2. Print_r after the json_decode call shows:

*********************
Step 3. Var dump of the decode result variable:
NULL

 

I think you may have introduced some additonal { down the bottom in your copy of the JSON? The original version doesn't have them. Try this:

[
{
"config_group_id":2,
"device_count":2,
"devices":
[
"11:22:00:00:00:01",
"11:22:00:00:00:02"
],
"name":"Bob",
"pause":false,
"profile_id":2,
"timespent":
{
"has_quota":false,
"quota":0,
"total_spent":
{
"normal":91,
"reward":0
}
},
"online_device_count":1,
"online":true
},
{
"config_group_id":3,
"device_count":2,
"devices":
[
"11:22:00:00:00:03",
"11:22:00:00:00:04"
],
"name":"Alex",
"pause":false,
"profile_id":3,
"timespent":
{
"has_quota":false,
"quota":0,
"total_spent":
{
"normal":0,
"reward":0
}
},
"online_device_count":0,
"online":false
},
{
"config_group_id":9,
"device_count":3,
"devices":
[
"11:22:00:00:00:05",
"11:22:00:00:00:06",
"11:22:00:00:00:07"
],
"name":"$lan$",
"pause":false,
"profile_id":4,
"timespent":
{
"has_quota":false,
"quota":0,
"total_spent":
{
"normal":638,
"reward":0
}
},
"online_device_count":2,
"online":true
}
] Edited by loquaci

mekodak - I'm assuming what you're referring to as 'state' is the value in the 'online' property. So, once you ingest the JSON return and parse the data, loop through the resulting array and print the 'online' index. The third record 'name' value looks weird, but potentially not incorrect.

morocco-iceberg, use the code button (<>) in the post editor toolbar.

Ok - yes I did.  Re-copied the json and it now runs for me.

So now the question from the op is "what is a state return".  What are you looking for OP?

If the return of the value of 'online' is desired I have managed to get this back:

Config group id 2 is True
Config group id 3 is False
Config group id 9 is True

And this is the code I used:

$decoded = json_decode($json_code, true);
foreach($decoded as $k=>$arr)
{
	$result = $arr['online'] == '1' ? 'True' :'False';
	echo "Config group id {$arr['config_group_id']} is $result<br>";
}

 

Edited by ginerjm

I got moste of it:
 

if($_REQUEST['name']) {
    	foreach(json_decode($json_code, true) as $k=>$user_id) {
    		if($user_id['name'] == $_REQUEST['name']) {
    			if($user_id['online'] == '1') {
    				echo TRUE;
    			} else if($user_id['online'] == '0') {
    				echo FALSE;
    			}
    		}
    	}
    }

But have problems requesting for the mac

I dont know if it is the arrey that is annoying me.

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.