Hi I'm currently trying to do an API request. The API sends out a json request like this:
[
{
"name": "Test 1"
"yes-or-no": "yes"
},
{
"name": "Test 2"
"yes-or-no": "no"
}
]
My question is, how do I select one of the yes-or-no
to echo in the website? I tried doing this:
<?php
$status = json_decode(file_get_contents('url to the json file'));
// Display message AKA uptime.
foreach ($status->yes-or-no as $answer) {
echo $answer.'<br />';
}
?>
But didn't work.
I'm sorry if I got some terms wrong, since I'm pretty new to coding APIs like this.
CodePudding user response:
Try something like this:
<?php
$statuses = json_decode(file_get_contents('url to the json file'));
foreach ($statuses as $status) {
echo $status->{'yes-or-no'};
}
?>
CodePudding user response:
I'm not really sure what you are trying to do, but maybe i can shed some light into the question:
$status = json_decode(file_get_contents('url to the json file'), true);
Add ", true" this will make your $status an array instead of an object.
foreach ($status as $answer) {
echo $answer['yes-or-no'].'<br />'; //output yes or no
echo $answer['name'].'<br />'; //output test 1 or test 2
}