title: PHP API saving JSON to file, then reading the file
body: <p>Having trouble with this.</p>
  
  <p>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.</p>
  
  <p>Anyway, I've logged into the API and retreived the data and can display the data "live" without an issue.</p>
  
  <p>The trouble starts when trying to save the data and read from the text file. It's a multi-depth array.</p>
  
  <p>The retreiving code</p>
  
  <pre><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" =&gt; "KEY","account" =&gt; "ACCOUNT" );
  curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  
  //make the request
  $result = curl_exec($ch);
  curl_close($ch);
  </code></pre>
  
  <p>This gets the code fine, and I can display</p>
  
  <pre><code>$properties = json_decode($result, true);
  $properties = $properties['properties'];
  //pr($parsed_json);
  
  foreach($properties as $key =&gt; $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']); 
  
          }
      }
  }
  </code></pre>
  
  <p>and this works.</p>
  
  <p>I then try and save the json to a text file with</p>
  
  <pre><code>file_put_contents('properties.txt', json_decode($result));
  </code></pre>
  
  <p>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?</p>
  
  <p>With JSONLint I validate the data and get the following error</p>
  
  <pre><code>Parse error on line 1:
  "{    \"status\": \
  ^
  Expecting '{', '['
  </code></pre>
  
  <p>Any ideas?</p>