mekodak Posted October 1, 2022 Share Posted October 1, 2022 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 } ] Quote Link to comment https://forums.phpfreaks.com/topic/315388-return-status-on-request/ Share on other sites More sharing options...
gw1500se Posted October 1, 2022 Share Posted October 1, 2022 (edited) You need to post your code. What did you try? What error did you get or what did you get that was not expected? Edited October 1, 2022 by gw1500se Quote Link to comment https://forums.phpfreaks.com/topic/315388-return-status-on-request/#findComment-1601243 Share on other sites More sharing options...
morocco-iceberg Posted October 2, 2022 Share Posted October 2, 2022 What are you deriving the state from? The online attribute? Quote Link to comment https://forums.phpfreaks.com/topic/315388-return-status-on-request/#findComment-1601251 Share on other sites More sharing options...
morocco-iceberg Posted October 2, 2022 Share Posted October 2, 2022 To start, you'll need a function that parses the JSON and finds the value you want to return Quote Link to comment https://forums.phpfreaks.com/topic/315388-return-status-on-request/#findComment-1601259 Share on other sites More sharing options...
ginerjm Posted October 2, 2022 Share Posted October 2, 2022 I very rarely use Json and do not profess any knowledge there. But when I put your json into a small php script and ran the json_decode on it, I get a null response which manual says indicates an error in the json itself. Quote Link to comment https://forums.phpfreaks.com/topic/315388-return-status-on-request/#findComment-1601263 Share on other sites More sharing options...
morocco-iceberg Posted October 2, 2022 Share Posted October 2, 2022 The JSON structure itself looks ok, you would definitely need to remove the `127.0.0.1 - 2022-10-01 13:13:20 - INFO -` header and only pass the information within the brackets with the result being an array of objects Quote Link to comment https://forums.phpfreaks.com/topic/315388-return-status-on-request/#findComment-1601264 Share on other sites More sharing options...
ginerjm Posted October 2, 2022 Share Posted October 2, 2022 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 Quote Link to comment https://forums.phpfreaks.com/topic/315388-return-status-on-request/#findComment-1601268 Share on other sites More sharing options...
morocco-iceberg Posted October 2, 2022 Share Posted October 2, 2022 (edited) 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 October 2, 2022 by loquaci Quote Link to comment https://forums.phpfreaks.com/topic/315388-return-status-on-request/#findComment-1601270 Share on other sites More sharing options...
morocco-iceberg Posted October 2, 2022 Share Posted October 2, 2022 Seems I've forgotten how to embed it as a code snippet, the usual markdown isn't working Quote Link to comment https://forums.phpfreaks.com/topic/315388-return-status-on-request/#findComment-1601271 Share on other sites More sharing options...
maxxd Posted October 2, 2022 Share Posted October 2, 2022 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. Quote Link to comment https://forums.phpfreaks.com/topic/315388-return-status-on-request/#findComment-1601275 Share on other sites More sharing options...
ginerjm Posted October 2, 2022 Share Posted October 2, 2022 (edited) 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 October 2, 2022 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/315388-return-status-on-request/#findComment-1601276 Share on other sites More sharing options...
mekodak Posted October 8, 2022 Author Share Posted October 8, 2022 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. Quote Link to comment https://forums.phpfreaks.com/topic/315388-return-status-on-request/#findComment-1601432 Share on other sites More sharing options...
ginerjm Posted October 8, 2022 Share Posted October 8, 2022 Do not use $_REQUEST. Use the appropriate array name to collect your input. You cannot echo a boolean value. Use a string instead. Quote Link to comment https://forums.phpfreaks.com/topic/315388-return-status-on-request/#findComment-1601434 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.