Current PHP Code is working but I need to save JSON as I mentioned in the example I need to access all data of array and save it inside the .JSON File
<?php
$curl = curl_init();
$fh = fopen('local.json','w');
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_URL, "https://jsonstorage.net/api/items/46b5a62d-9a39-41ab-89f4-e75f458c9189");
curl_setopt($curl, CURLOPT_FILE, $fh);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$contents = curl_exec($curl);
fwrite($fh, $contents);
curl_close($curl);
fclose($fh);
?>
My JSON data is in this form but I need to access all data of [data]
{
"data": [
{
"adapterid": 44835,
"rowid": 1573784208932,
"battery": 3610,
"createddate": "15-11-2019",
"gid": "01:f0:50:11:a1:35:87",
"id": 2277491836402479600,
"projectid": 32107,
"rssi": -90,
"temp": 25.75
},
{
"adapterid": 44835,
"rowid": 1573784212032,
"battery": 3660,
"createddate": "15-11-2019",
"gid": "01:f0:50:11:a1:35:87",
"id": 2277491836402479600,
"projectid": 32107,
"rssi": -89,
"temp": 25.75
},
{
"adapterid": 44835,
"rowid": 1573784215034,
"battery": 3610,
"createddate": "15-11-2019",
"gid": "01:f0:50:11:a1:35:87",
"id": 2277491836402479600,
"projectid": 32107,
"rssi": -96,
"temp": 25.75
}
]
}
the response I want is:
{
"data": [
**I want this data**
]
}
here is the example sample of my requirement:
[
{
"adapterid": 44835,
"rowid": 1573784208932,
"battery": 3610,
"createddate": "15-11-2019",
"gid": "01:f0:50:11:a1:35:87",
"id": 2277491836402479600,
"projectid": 32107,
"rssi": -90,
"temp": 25.75
},
{
"adapterid": 44835,
"rowid": 1573784212032,
"battery": 3660,
"createddate": "15-11-2019",
"gid": "01:f0:50:11:a1:35:87",
"id": 2277491836402479600,
"projectid": 32107,
"rssi": -89,
"temp": 25.75
},
{
"adapterid": 44835,
"rowid": 1573784215034,
"battery": 3610,
"createddate": "15-11-2019",
"gid": "01:f0:50:11:a1:35:87",
"id": 2277491836402479600,
"projectid": 32107,
"rssi": -96,
"temp": 25.75
}
]
How can I access this? Thanks in advance
Try this:
$contents = json_decode($contents,1);
print_r($contents['data']);
You can turn the json into an associative array using json_decode($contents, true);
This allows you to retrieve the data
array inside the json, then turn it back into json using json_encode($contents['data']);
to write the data to file:
<?php
$curl = curl_init();
$fh = fopen('local.json', 'w');
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_URL, "https://jsonstorage.net/api/items/46b5a62d-9a39-41ab-89f4-e75f458c9189");
curl_setopt($curl, CURLOPT_FILE, $fh);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$contents = curl_exec($curl);
$contents = json_decode($contents, true); // to associative array
$contents = json_encode($contents['data']); // to json, only the 'data' array
fwrite($fh, $contents); // write json 'data' array to file
curl_close($curl);
fclose($fh);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.