How do I print the success message - php

How do I print the success message in this JSON code using PHP
{
"status": 0,
" result":{
"status":"success"
}
}

$response = {"status":"success"};
$data = json_decode(response ,TRUE); //converts to associative array
if($data['status'] == 'success'){
echo 'You are success';
}

$result = json_decode('{
"status": 0,
" result":{
"status":"success"
}
}');
echo $result['result']['status'];

Convert this json to PHP Array using json_decode()
Check the ['result']['status']
if its equals to 'success'
Then print some message

Related

Equality comparison PHP

I'm doing this comparison below: $deviceKey == 'mobile' or $deviceKey == 'desktop'
public function syncPublisherIds (string $publisherId, Request $request):array
{
file_put_contents('/var/log/php/burner.log', json_encode($request->getContent()) . PHP_EOL, FILE_APPEND);
$idSyncConfig = json_decode($request->getContent());
$idSyncResponseObject = new \stdClass();
foreach($idSyncConfig as $deviceKey => $deviceValue) {
// file_put_contents('/var/log/php/burner.log', json_encode(gettype($deviceKey)) . ' '. json_encode($deviceValue) . PHP_EOL, FILE_APPEND);
// $deviceKey = json_encode($deviceKey);
// add device type to object
if ($deviceKey == 'mobile' or $deviceKey == 'desktop') {
$idSyncResponseObject[$deviceKey] = new \stdClass();
} else {
throw ((new BadRequestException("Platforms must be of type 'mobile' or 'desktop', you sent type of: $deviceKey"))->errorize());
}
}
}
For some reason it hits the else statement when they are seemingly the same:
"Platforms must be of type 'mobile' or 'desktop', you sent type of: \"mobile\""
This is a Laravel endpoint.
<?php
function testForLuke(string $requestContent): void
{
$idSyncConfig = json_decode(stripslashes($requestContent));
//print_r([$requestContent, $idSyncConfig]);
foreach ($idSyncConfig as $deviceKey => $deviceValue) {
//print_r([$deviceKey, $deviceValue]);
if ($deviceKey === 'mobile' || $deviceKey === 'desktop') {
echo "great success with deviceKey of '${deviceKey}'!\n";
} else {
echo "error condition throw exception\n";
}
}
}
testForLuke('{ "mobile": { "id": 1 } }');
testForLuke('{ "desktop": { "id": 1 } }');
testForLuke('{ "derptop": { "id": 1 } }');
testForLuke("{ \"mobile\": { \"id\": 1 } }");
testForLuke("{ \"desktop\": { \"id\": 1 } }");
For me yields:
great success with deviceKey of 'mobile'!
great success with deviceKey of 'desktop'!
error condition throw exception
great success with deviceKey of 'mobile'!
great success with deviceKey of 'desktop'!
The code seems to be working already, but is highly dependent on the structure of the provided JSON, so depending on the structure of the request you may still have issues, let us know.
Edit: After re-looking, it seems like you've got backslashes in your JSON request. Are you encoding it yourself? If so you should use json_encode($response, JSON_UNESCAPED_SLASHES);
If you can't control how it is encoded for whatever you reason, you could consider using:
$idSyncConfig = json_decode(stripslashes($requestContent));

PHP JSON array issue

I have tryed almost everything to get the data in observations in a foreach sentence in php but can get it to output any data.
I Want to get the observations rows listed so i can insert it into a MySQL DB
{
"version":"2.0","secret":"meraki","type":"DevicesSeen","data":{
"apMac":"88:15:44:84:4c:90","apFloors":[],"apTags":["","recently-added",""]
,"observations":[{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":0.2907945825483442,"x":[],"y":[]},"seenTime":"2017-05-20T12:24:43Z","ssid":null,"os":"Cisco/Linksys Router","clientMac":"94:9f:3e:06:c1:6c","seenEpoch":1495283083,"rssi":47,"ipv6":null,"manufacturer":"Sonos"},{
"ipv4":"/10.226.198.65","location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":5.374873752610671,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:30Z","ssid":"DanfossConnect","os":"Slingbox","clientMac":"00:11:f6:f3:43:06","seenEpoch":1495283130,"rssi":46,"ipv6":null,"manufacturer":"Asia Pacific Microsystems"},{
"ipv4":"/172.16.0.25","location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":33.788906850703945,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:29Z","ssid":"Wireless WiFi","os":"HP Printer","clientMac":"c4:34:6b:08:dc:6d","seenEpoch":1495283129,"rssi":13,"ipv6":null,"manufacturer":"Hewlett-Packard"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":14.90854452921782,"x":[],"y":[]},"seenTime":"2017-05-20T12:24:45Z","ssid":null,"os":null,"clientMac":"54:60:09:0e:08:d2","seenEpoch":1495283085,"rssi":31,"ipv6":null,"manufacturer":"Google"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":0.287722117087862,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:54Z","ssid":null,"os":null,"clientMac":"94:9f:3e:06:c1:6d","seenEpoch":1495283154,"rssi":46,"ipv6":null,"manufacturer":"Sonos"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":4.189862222398972,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:49Z","ssid":null,"os":"Cisco/Linksys Router","clientMac":"5c:aa:fd:40:29:78","seenEpoch":1495283149,"rssi":41,"ipv6":null,"manufacturer":"Sonos"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":38.279358673491906,"x":[],"y":[]},"seenTime":"2017-05-20T12:23:18Z","ssid":null,"os":null,"clientMac":"f4:42:8f:c3:84:8b","seenEpoch":1495282998,"rssi":16,"ipv6":null,"manufacturer":"Samsung"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":39.49058481097055,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:28Z","ssid":null,"os":null,"clientMac":"a0:8c:fd:06:ec:17","seenEpoch":1495283128,"rssi":9,"ipv6":null,"manufacturer":"Hewlett-Packard"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":4.966494178173178,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:49Z","ssid":null,"os":null,"clientMac":"5c:aa:fd:40:29:79","seenEpoch":1495283149,"rssi":40,"ipv6":null,"manufacturer":"Sonos"},{
"ipv4":null,"location":{"lat":56.46452206894476,"lng":9.39005605565066,"unc":28.907921961158962,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:22Z","ssid":null,"os":"iOS","clientMac":"b0:70:2d:84:38:cf","seenEpoch":1495283122,"rssi":13,"ipv6":null,"manufacturer":"Apple"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":40.978157267912586,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:34Z","ssid":null,"os":null,"clientMac":"60:6d:c7:85:91:17","seenEpoch":1495283134,"rssi":20,"ipv6":null,"manufacturer":"Hon Hai/Foxconn"}
]
}
}
https://jsonlint.com/ Your json code has syntax problem! one } was missing! Then go ahead with the other answers :)
<?php
$something = json_decode('{
"version":"2.0",
"secret":"meraki",
"type":"DevicesSeen",
"data":{
"apMac":"88:15:44:84:4c:90",
"apFloors":[],
"apTags":["","recently-added",""],
"observations":[{
"ipv4":null,
"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":0.2907945825483442,"x":[],"y":[]},
"seenTime":"2017-05-20T12:24:43Z",
"ssid":null,
"os":"Cisco/Linksys Router",
"clientMac":"94:9f:3e:06:c1:6c",
"seenEpoch":1495283083,
"rssi":47,
"ipv6":null,
"manufacturer":"Sonos"
},{
"ipv4":"/10.226.198.65","location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":5.374873752610671,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:30Z","ssid":"DanfossConnect","os":"Slingbox","clientMac":"00:11:f6:f3:43:06","seenEpoch":1495283130,"rssi":46,"ipv6":null,"manufacturer":"Asia Pacific Microsystems"},{
"ipv4":"/172.16.0.25","location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":33.788906850703945,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:29Z","ssid":"Wireless WiFi","os":"HP Printer","clientMac":"c4:34:6b:08:dc:6d","seenEpoch":1495283129,"rssi":13,"ipv6":null,"manufacturer":"Hewlett-Packard"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":14.90854452921782,"x":[],"y":[]},"seenTime":"2017-05-20T12:24:45Z","ssid":null,"os":null,"clientMac":"54:60:09:0e:08:d2","seenEpoch":1495283085,"rssi":31,"ipv6":null,"manufacturer":"Google"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":0.287722117087862,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:54Z","ssid":null,"os":null,"clientMac":"94:9f:3e:06:c1:6d","seenEpoch":1495283154,"rssi":46,"ipv6":null,"manufacturer":"Sonos"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":4.189862222398972,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:49Z","ssid":null,"os":"Cisco/Linksys Router","clientMac":"5c:aa:fd:40:29:78","seenEpoch":1495283149,"rssi":41,"ipv6":null,"manufacturer":"Sonos"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":38.279358673491906,"x":[],"y":[]},"seenTime":"2017-05-20T12:23:18Z","ssid":null,"os":null,"clientMac":"f4:42:8f:c3:84:8b","seenEpoch":1495282998,"rssi":16,"ipv6":null,"manufacturer":"Samsung"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":39.49058481097055,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:28Z","ssid":null,"os":null,"clientMac":"a0:8c:fd:06:ec:17","seenEpoch":1495283128,"rssi":9,"ipv6":null,"manufacturer":"Hewlett-Packard"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":4.966494178173178,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:49Z","ssid":null,"os":null,"clientMac":"5c:aa:fd:40:29:79","seenEpoch":1495283149,"rssi":40,"ipv6":null,"manufacturer":"Sonos"},{
"ipv4":null,"location":{"lat":56.46452206894476,"lng":9.39005605565066,"unc":28.907921961158962,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:22Z","ssid":null,"os":"iOS","clientMac":"b0:70:2d:84:38:cf","seenEpoch":1495283122,"rssi":13,"ipv6":null,"manufacturer":"Apple"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":40.978157267912586,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:34Z","ssid":null,"os":null,"clientMac":"60:6d:c7:85:91:17","seenEpoch":1495283134,"rssi":20,"ipv6":null,"manufacturer":"Hon Hai/Foxconn"}]
}
}');
foreach($something->data->observations[0] as $key => $val){
if($key === "location"){
echo '<hr><h4>here is the location object</h4>';
foreach($something->data->observations[0]->$key as $item => $data){
print_r(['item'=>$item,'data'=>$data]);
}
echo '<hr>';
}
print_r($something->data->observations[0]->$key);
foreach($something->data->observations[0]->$key as $item => $data){
if($item === '')
print_r(['item'=>$item,'data'=>$data]);
}
print_r(['key'=>$key,'val'=>$val]);
echo '<br>';
}
echo '<hr>';
print_r($something->data->observations[0]);
echo '<hr>';
print_r($something);
echo '<hr>';
exit;
<?php
// you have to use json_decode for sure
$something = json_decode('{
"version":"2.0","secret":"meraki","type":"DevicesSeen","data":{
"apMac":"88:15:44:84:4c:90","apFloors":[],"apTags":["","recently-added",""]
,"observations":[{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":0.2907945825483442,"x":[],"y":[]},"seenTime":"2017-05-20T12:24:43Z","ssid":null,"os":"Cisco/Linksys Router","clientMac":"94:9f:3e:06:c1:6c","seenEpoch":1495283083,"rssi":47,"ipv6":null,"manufacturer":"Sonos"},{
"ipv4":"/10.226.198.65","location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":5.374873752610671,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:30Z","ssid":"DanfossConnect","os":"Slingbox","clientMac":"00:11:f6:f3:43:06","seenEpoch":1495283130,"rssi":46,"ipv6":null,"manufacturer":"Asia Pacific Microsystems"},{
"ipv4":"/172.16.0.25","location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":33.788906850703945,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:29Z","ssid":"Wireless WiFi","os":"HP Printer","clientMac":"c4:34:6b:08:dc:6d","seenEpoch":1495283129,"rssi":13,"ipv6":null,"manufacturer":"Hewlett-Packard"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":14.90854452921782,"x":[],"y":[]},"seenTime":"2017-05-20T12:24:45Z","ssid":null,"os":null,"clientMac":"54:60:09:0e:08:d2","seenEpoch":1495283085,"rssi":31,"ipv6":null,"manufacturer":"Google"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":0.287722117087862,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:54Z","ssid":null,"os":null,"clientMac":"94:9f:3e:06:c1:6d","seenEpoch":1495283154,"rssi":46,"ipv6":null,"manufacturer":"Sonos"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":4.189862222398972,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:49Z","ssid":null,"os":"Cisco/Linksys Router","clientMac":"5c:aa:fd:40:29:78","seenEpoch":1495283149,"rssi":41,"ipv6":null,"manufacturer":"Sonos"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":38.279358673491906,"x":[],"y":[]},"seenTime":"2017-05-20T12:23:18Z","ssid":null,"os":null,"clientMac":"f4:42:8f:c3:84:8b","seenEpoch":1495282998,"rssi":16,"ipv6":null,"manufacturer":"Samsung"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":39.49058481097055,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:28Z","ssid":null,"os":null,"clientMac":"a0:8c:fd:06:ec:17","seenEpoch":1495283128,"rssi":9,"ipv6":null,"manufacturer":"Hewlett-Packard"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":4.966494178173178,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:49Z","ssid":null,"os":null,"clientMac":"5c:aa:fd:40:29:79","seenEpoch":1495283149,"rssi":40,"ipv6":null,"manufacturer":"Sonos"},{
"ipv4":null,"location":{"lat":56.46452206894476,"lng":9.39005605565066,"unc":28.907921961158962,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:22Z","ssid":null,"os":"iOS","clientMac":"b0:70:2d:84:38:cf","seenEpoch":1495283122,"rssi":13,"ipv6":null,"manufacturer":"Apple"},{
"ipv4":null,"location":{"lat":56.46451920053334,"lng":9.390050682697847,"unc":40.978157267912586,"x":[],"y":[]},"seenTime":"2017-05-20T12:25:34Z","ssid":null,"os":null,"clientMac":"60:6d:c7:85:91:17","seenEpoch":1495283134,"rssi":20,"ipv6":null,"manufacturer":"Hon Hai/Foxconn"}
]
}
}',true);
// first of all you miss an {
// use the second argument "true" to make it return an array
foreach ( $something['data']['observations'] as $item ){
$location_lng = $item['location']['lng'];
$location_lat = $item['location']['lat'];
echo $location_lng;
echo '<hr>';
echo $location_lat;
// Everything is good :)
}
?>

how to capture json returned value in php

I'm writing a script to make an api call and add credit card to a billing system
<?php
include 'class.hbwrapper.php';
HBWrapper::setAPI('http://my-site.com/admin/api.php','API ID','API Key');
$params = array(
'id'=>ID,
'cardnum'=>CARDNUM,
'cardtype'=>CARDTYPE,
'expiryyear'=>EXPIRYYEAR,
'expirymonth'=>EXPIRYMONTH
);
$return = HBWrapper::singleton()->editClientCreditCard($params);
print_r($return);
?>
Here is the response
{
"success": true,
"call": "editClientCreditCard",
"server_time": 1317726229,
"info": [
"Credit Card details updated"
]
}
How can I write an if statement to check the value of returned success value if it is either true or false in php?
in another question, should I be looking at php to do this? or use jquery?
Thank you
Depending on your PHP version:
For an object:
if(json_decode($return)->success === true)
For an array:
if(json_decode($return, true)['success'] === true)
For older PHP or if you need the entire object/array decoded:
$result = json_decode($return);
if($result->success === true)
Like this;
$data = json_decode($return);
if($data['success'] === true) {
//true
} else {
//false
}
You can use json_decode to transform the JSON response to a php variable. Then you can check the response.
For further information look at the PHP manual http://php.net/manual/de/function.json-decode.php
$data=json_decode($your_json_string);
if($data->success){
...
}
you can do it like this .
$return = json_decode($return);
if($return->success){
// true
echo "TRUE";
} else {
// false
echo "FALSE";
}

GET JSON provides two values, I want to add these two togther using php, and then return to callback

$_GET['numberofwelds']; & $_GET['numberofconwelds']; are sent to this script using GET JSON. I want to add these together, and then use json_encode to send a total back to the callback (in another php script) . If both $_GET 's are empty, then I want nothing to happen. How should I change this?
$numberofwelds = $_GET['numberofwelds'];
$numberofconwelds = $_GET['numberofconwelds'];
if (isset($_GET['numberofwelds']) && $_GET['numberofwelds'] != '' {
$numberofwelds + $numberofconwelds = $sum_total;
echo json_encode($sumtotal);
} else {
exit()
}
Firstly, you are trying to access your $_GET variables without checking they exist first.
Secondly, you should be throwing Exceptions instead of just calling exit() or die(). You can then log them with $e->getMessage() or write them to the local filesystem.
Finally, you need to validate your data. Make sure it is what you expect it to be.
if (isset($_GET['numberofwelds']) && isset($_GET['numberofconwelds']))
{
// Now we know both values definitely exist, VALIDATE them
$numwelds = $_GET['numberofwelds'];
$numconwelds = $_GET['numberofconwelds'];
if (is_int($numwelds) && is_int($numconwelds))
{
// Calculate your total
$total = $numwelds + $numconwelds;
echo json_encode($total);
}
else
{
// We get here because your GET variables do exist but they aren't
// numbers as you expect (you or someone else has sent rubbish data)
// You want to do nothing, although I would return an error in your json
// to be displayed to the user or logged by the consumer of the service
}
}
else
{
// We get here because your GET variables simply don't exist. They haven't been
// passed in as you are expecting them to be
// You want to do nothing, although I would return an error in your json
// to be displayed to the user or logged by the consumer of the service
}
Always code defensively.
I'm going to show you what I would do in this situation.
if (isset($_GET['numberofwelds']) && isset($_GET['numberofconwelds']))
{
$numwelds = $_GET['numberofwelds'];
$numconwelds = $_GET['numberofconwelds'];
if (is_int($numwelds) && is_int($numconwelds))
{
$total = $numwelds + $numconwelds;
$response = array("status" => "success", "message" => $total);
echo $response;
}
else
{
$response = array("status" => "failure", "message" => "GET params were not numbers");
echo $response;
}
}
else
{
$response = array("status" => "failure", "message" => "GET params do not exist");
echo $response;
}
Then, in your consuming service (most likely a JavaScript / jQuery AJAX call), you can do the following:
.done(function(data) {
var json = $.parseJSON(data);
if (data.status === "success") {
// Yay, we got a success back
console.log("The total is: " + data.message);
} else if (data.status === "failure") {
// Uh oh, something's gone wrong server-side
console.log(data.message);
}
});
change this
if ($_GET['numberofwelds'] != '' && $_GET['numberofconwelds'] != '') {
$numberofwelds + $numberofconwelds = $sum_total;
echo json_encode($sumtotal);
} else {
exit()
}
to this
if ($numberofwelds && $numberofconwelds ) {
$sum_total = array(
'sumTotal' => $numberofwelds + $numberofconwelds,
);
echo json_encode($sum_total);
}else {
exit();
}
Please always check existence of the array keys with isset() construction or array_key_exists() function.
if (isset($_GET['numberofwelds']) && $_GET['numberofwelds'] != '' && isset($_GET['numberofconwelds']) && $_GET['numberofconwelds'] != '') {
echo json_encode(array("total" => $_GET['numberofwelds'] + $_GET['numberofconwelds']));
} else {
exit();
}
UPDATE
With is_numeric() function this code should be more robust:
if (isset($_GET['numberofwelds']) && is_numeric($_GET['numberofwelds']) && isset($_GET['numberofconwelds']) && is_numeric($_GET['numberofconwelds'])) {
echo json_encode(array("total" => $_GET['numberofwelds'] + $_GET['numberofconwelds']));
} else {
exit();
}
PHP reference: array_key_exists()
PHP reference: isset()
$numberofwelds = json_decode($_GET['numberofwelds'], true);
$numberofconwelds = json_decode($_GET['numberofconwelds'], true);
$mergedJson = array_merge(numberofwelds, numberofconwelds);
echo json_encode($mergedJson);
This should do it. It grabs the json, decodes and turns it in to an array (second parameter of json_decode set to true) and then combines them.

make php variable from plain text get file

currently i am using the below code to get a file from a site which tells me the current server status of a game server. the file is in plain text format and out puts the following depending on server status:
ouput:
{ "state": "online", "numonline": "185" }
or
{ "state": "offline" }
or
{ "state": "error" }
file get code:
<?php
$value=file_get_contents('http:/example.com/server_state.aspx');
echo $value;
?>
I would like to turn the 'state' and 'numonline' into their own variables so i could output them using a if, like:
<?php
$content=file_get_contents('http://example.com/server_state.aspx');
$state <--- what i dont know how to make
$online <--- what i dont know how to make
if ($state == online) {
echo "Server: $state , Online: $online";
} else {
echo "Server: Offline";
)
?>
but i have no idea how to turn the 'state' and 'numonline' from the plain text into a variable of their own ($state and $online), how would i go about doing this?
Your data is JSON. Use json_decode to parse it into a usable form:
$data = json_decode(file_get_contents('http:/example.com/server_state.aspx'));
if (!$data) {
die("Something went wrong when reading or parsing the data");
}
switch ($data->state) {
case 'online':
// e.g. echo $data->numonline
case 'offline':
// ...
}
Use json_decode function:
$value = '{ "state": "online", "numonline": "185" }';
$json = json_decode($value, true);
print_r($json);
if ($json['state'] == 'online') {
echo "Server: " . $json['state'] . " , Online: " . $json['numonline'];
} else {
echo "Server: Offline";
}
Output:
Array
(
[state] => online
[numonline] => 185
)
I would like to turn the 'state' and 'numonline' into their own variables:
Maybe you are looking for extract,
Example:
$value = '{ "state": "online", "numonline": "185" }';
$json = json_decode($value, true);
extract($json);
//now $state is 'online' and $numonline is 185

Categories