Skip to content

Instantly share code, notes, and snippets.

@davidsword
Last active February 22, 2021 21:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidsword/e89955cbc439dc3a8856c64d56954094 to your computer and use it in GitHub Desktop.
Save davidsword/e89955cbc439dc3a8856c64d56954094 to your computer and use it in GitHub Desktop.
<?
// settings
$url = "https://example.com/queryforjson"; // json source
$cache = __DIR__."/json.cache"; // make this file in same dir
$force_refresh = false; // dev
$refresh = 60*60; // once an hour
// cache json results so to not over-query (api restrictions)
if ($force_refresh || ((time() - filectime($cache)) > ($refresh) || 0 == filesize($cache))) {
// read json source
$ch = curl_init($url) or die("curl issue");
$curl_options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_FOLLOWLOCATION => false,
CURLOPT_ENCODING => "",
CURLOPT_AUTOREFERER => true,
CURLOPT_CONNECTTIMEOUT => 7,
CURLOPT_TIMEOUT => 7,
CURLOPT_MAXREDIRS => 3,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_USERAGENT => "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.A.B.C Safari/525.13"
);
curl_setopt_array($ch, $curl_options);
$curlcontent = curl_exec( $ch );
curl_close( $ch );
$handle = fopen($cache, 'wb') or die('no fopen');
$json_cache = $curlcontent;
fwrite($handle, $json_cache);
fclose($handle);
} else {
$json_cache = file_get_contents($cache); //locally
}
$jsonobject = new SimpleXMLElement($json_cache);
// do something with $jsonobject
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment