Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6924407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:39:33+00:00 2026-05-27T10:39:33+00:00

I am currently using a PHP script to create a cache of JSON files.

  • 0

I am currently using a PHP script to create a cache of JSON files. We have a PHP file on our server that queries a very large database, and returns result sets in JSON format (so we can use it on the site in jQuery and other frameworks).

The script for presenting the raw JSON from our database works great, and I made a simple cache building script that also works to a degree. However, we have noticed some odd things happening with the resulting cache file.

PHP seems to be adding slashes to the quote marks, as well as adding superfluous " to the beginning and end of the JSON.

Here is a sample of the JSON we’re passing in (note that it’s not complete):

[
   {
      "id":1580,
      "name":"Fydell House",
      "address1":"South Square",
      "address2":null,
      "towncity":"Boston",
      "county":"Lincolnshire",
      "postcode":"PE21 6HU",
      "addressVerbose":"South Square
    Boston
    Lincolnshire
    PE21 6HU
    ",
      "addressVerboseLinked":"",
      "longitude":-0.022778,
      "latitude":52.975806,
      "londonBorough":null,
      "telno":"01205 351 520",
      "faxno":null,
      "email":null,
      "webaddress":null,
      "mainimg":null,
      "venueType":"Historic Building",
      "description":null,
      "excerpt":" ",
      "images":null,
      "creationDate":943920000,
      "londonfeatured":false,
      "unusual":false,
      "featured":false,
      "active":true,
      "trial":false,
      "modifiedDate":1234709308,
      "hits":"1579",
      "allowReviews":false,
      "amenities":null,
      "imagealt":"Lincolnshire wedding reception venue in Boston, Fydell House",
      "imagetitle":"Lincolnshire wedding venues in Boston",
      "car_directions":null,
      "pub_directions":null,
      "additional_info":null,
      "listedBy":0,
      "listedByName":null,
      "region":null
   }
]

And the PHP code that outputs the JSON file and stores it on our server:

// Function to output the contents from the live data, and create a cache file:
function create_cache_file($url, $filename)
{
    $url = str_replace(' ', '%20', $url);
    $json_string = file_get_contents($url);
    $file = fopen($filename, 'w');

    // If there is a problem creating the file:
    if(!$file)
    {
        die('error creating the file!');
    }
    else
    {
        fwrite($file, json_encode($json_string));
        fclose($file);
        echo $json_string;
    }
}

And this is what the file looks like after it’s been processed with PHP and stored on our server:

"[{\"id\":437,\"name\":\"Lanteglos Country House Hotel\",\"address1\":\"Lanteglos-by-Camelford\",\"address2\":null,\"towncity\":\"Camelford\",\"county\":\"Cornwall\",\"postcode\":\"PL32 9RF\",\"addressVerbose\":\"Lanteglos-by-Camelford<br \\\/>Camelford<br \\\/>Cornwall<br \\\/>PL32 9RF<br \\\/>\",\"addressVerboseLinked\":\"\",\"longitude\":-4.695491,\"latitude\":50.612462,\"londonBorough\":null,\"telno\":\"01840 213 551\",\"faxno\":null,\"email\":null,\"webaddress\":null,\"mainimg\":null,\"venueType\":\"Hotel\",\"description\":null,\"excerpt\":\"                                   \",\"images\":null,\"creationDate\":943920000,\"londonfeatured\":false,\"unusual\":false,\"featured\":false,\"active\":true,\"trial\":false,\"modifiedDate\":1234662248,\"hits\":\"1145\",\"allowReviews\":false,\"amenities\":null,\"imagealt\":\"Cornwall wedding reception venue in Camelford, Lanteglos Country House Hotel\",\"imagetitle\":\"Cornwall wedding venues in Camelford\",\"car_directions\":null,\"pub_directions\":null,\"additional_info\":null,\"listedBy\":0,\"listedByName\":null,\"region\":null},{\"id\":438,\"name\":\"Rosehill Public House\",\"address1\":\"Little Petherick\",\"address2\":null,\"towncity\":\"Wadebridge\",\"county\":\"Cornwall\",\"postcode\":\"PL27 7QT\",\"addressVerbose\":\"Little Petherick<br \\\/>Wadebridge<br \\\/>Cornwall<br \\\/>PL27 7QT<br \\\/>\",\"addressVerboseLinked\":\"\",\"longitude\":-4.94093,\"latitude\":50.51259,\"londonBorough\":null,\"telno\":\"01841 540 777\",\"faxno\":null,\"email\":null,\"webaddress\":null,\"mainimg\":null,\"venueType\":\"Inn \\\/ Pub\",\"description\":null,\"excerpt\":\"                                   \",\"images\":null,\"creationDate\":943920000,\"londonfeatured\":false,\"unusual\":false,\"featured\":false,\"active\":true,\"trial\":false,\"modifiedDate\":1234752874,\"hits\":\"818\",\"allowReviews\":false,\"amenities\":null,\"imagealt\":\"Cornwall wedding reception venue in Wadebridge, Rosehill Public House\",\"imagetitle\":\"Cornwall wedding venues in Wadebridge\",\"car_directions\":null,\"pub_directions\":null,\"additional_info\":null,\"listedBy\":0,\"listedByName\":null,\"region\":null}]"

This is causing some serious problems elsewhere on the site when we try to decode the JSON. While we can use stripslashes to remove the slashes, some of the other areas are also causing parse errors (that weren’t present using the raw data served up by the first PHP file).

Can anyone suggest why PHP is adding slashes and erroneous quote marks around the string? Ideally, we would like a work-around at the point of creating the JSON file on the server, not reading it from it…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:39:34+00:00Added an answer on May 27, 2026 at 10:39 am

    I take it that file_get_contents($url) returns you a JSON encoded file? Then your problem is that you’re JSON encoding it again. "[{\"id\":437,\"nam... is the proper JSON encoded representation of the string [{"id":1580,"nam....

    If you json_decoded it when reading from the cache file, you’d get the original JSON string back. Or just don’t json_encode it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a php script im currently using that creates thumbnails based on a
I currently have a very simple intranet that we are using for some basic
I'm currently using PHP, JAVASCRIPT, MYSQL, XHTML, CSS to develop my site. Note that
I'm currently developing a PHP application that's using an Access database as a backend.
I'm currently using PHP to dynamically create a javascript which will be echoed on
I have a script on my site ('write-review.php') that takes an optional url parameter
Trying to copy a database into a new database, using a PHP script that
I'm currently using the following script-- <?php // Set the content-type header('Content-type: image/png'); //
I'd like to create a php script that runs as a daily cron. What
I am trying to create a php script that will retrieve the WOW factor

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.