Skip to content

Instantly share code, notes, and snippets.

@ksafranski
Created June 16, 2012 02:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ksafranski/2939705 to your computer and use it in GitHub Desktop.
Save ksafranski/2939705 to your computer and use it in GitHub Desktop.
Encapsulate JSON in PHP
/*
* This script allows saving & retrieval of JSON data in .php files by
* encapsulating in a <?php/*| ... |*/?> closure, preventing them from
* being accessed via URL.
*/
//////////////////////////////////////////////////////////////////////
// Get JSON
/////////////////////////////////////////////////////////////////////
function getJSON($file){
$json = file_get_contents($file);
$json = str_replace("|*/?>","",str_replace("<?php/*|","",$json));
$json = json_decode($json,true);
return $json;
}
//////////////////////////////////////////////////////////////////////
// Save JSON
//////////////////////////////////////////////////////////////////////
function saveJSON($file,$data){
$data = "<?php/*|" . json_encode($data) . "|*/?>";
$write = fopen($file, 'w') or die("can't open file");
fwrite($write, $data);
fclose($write);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment