Having trouble with this.
Basically, I'm trying to access an API to retrieve data. I'm limited to a certain number of connects, so my plan was to retrieve the data, save it to a text file on my host, and then read from that file as much as I want. I'd then use a cron job to re-populate the file with new data every few hours.
Anyway, I've logged into the API and retreived the data and can display the data "live" without an issue.
The trouble starts when trying to save the data and read from the text file. It's a multi-depth array.
The retreiving code
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, 'URL GOES HERE');
//prepare the field values being posted to the service
$data = array("appkey" => "KEY","account" => "ACCOUNT" );
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
//make the request
$result = curl_exec($ch);
curl_close($ch);
This gets the code fine, and I can display
$properties = json_decode($result, true);
$properties = $properties['properties'];
//pr($parsed_json);
foreach($properties as $key => $value) {
if ($_GET[section] == "sale") {
if ($value['category_id'] == "1") {
displayProp ($address, $value['price'], $value['qualifier_name'], $value['summary']);
}
} elseif ($_GET[section] == "rent") {
if ($value['category_id'] == "2") {
displayProp ($address, $value['price'], $value['freq_name'], $value['summary']);
}
}
}
and this works.
I then try and save the json to a text file with
file_put_contents('properties.txt', json_decode($result));
which saves the data in the file okay. But when trying to read from it, I get random errors no matter what I try. Could someone help with reading the text file and outputting the array?
With JSONLint I validate the data and get the following error
Parse error on line 1:
"{ \"status\": \
^
Expecting '{', '['
Any ideas?