5
\$\begingroup\$

display.php:

<?php
include "funtc.php";
updater();
$token = json_decode(file_get_contents('wp-content/themes/World-of-Warcraft-New/includes/token.json'));//<--open data file
$data = $token->update->NA->formatted;//<--store primary object 
if(!isset($data, $data->buy,$data->{'24min'},$data->{'24max'},$data->timeToSell,$data->updated)){ //<-- check each object property for data
unset ($token);//<-- If false clear variable
$old = json_decode(file_get_contents('wp-content/themes/World-of-Warcraft-New/includes/back.json'));//<--open backup data file
$data = $old->update->NA->formatted;
badfile();
}
?>
<!-- Start WOW token data import--> 
 <div id="tokenbox">
 <p class="gold">Buy:&nbsp;<?php echo chop($data->buy,"g"); ?></p>
 <p>24-hour change:&nbsp;<?php echo chop($data->{'24min'},"g"); ?><span class="gold">&nbsp;&nbsp;&nbsp;</span> - <?php echo chop($data->{'24max'},"g"); ?><span class="gold">&nbsp;&nbsp;&nbsp;</span></p>
 <p>Time To Sell:&nbsp;<?php echo chop($data->timeToSell,"g"); ?></p>
 <p>Updated:&nbsp;<?php echo chop($data->updated,"EDT"); ?> <br><a href="https://wowtoken.info">WOWToken</a></p>
 </div>
<!-- End WOW token data import--> 

This is the file that is use read json file and display data. I used the "!isset" function to check if all the object property's are vaild and have data because the file creator tends to change the file often. In the event that the format does change there is backup file that script can use to display the lasted use data file before the new was grabbed.

funtc.php:

//updater
function updater(){
 $now = strtotime('now'); //<--store current unix timestamp
 $stat = json_decode(file_get_contents('wp-content/themes/World-of-Warcraft-New/includes/stat.json'),true);//<--open updater status file
 $opts =stream_context_create(array( 'http' => array( 'timeout' =>3, 'user_agent'=> "The WarCraft Call Board - http://swcallboard.com")));
 if ($now > $stat['update'] && $stat['heath']=="alive" ){// <--check if currnet timestamp is greater than last updated timestamp and if no errors recorded
 $file = file_get_contents("https://wowtoken.info/wowtoken.json", 0, $opts); //<--grab remote file if check clears 
 if($file){
 rename("wp-content/themes/World-of-Warcraft-New/includes/token.json", "wp-content/themes/World-of-Warcraft-New/includes/back.json");//<-creates backup file
 file_put_contents("wp-content/themes/World-of-Warcraft-New/includes/token.json",$file );//<--writes remote file data to local file
 $stat['update']= strtotime('+20 minute');//<-- sets the "update" value of status file to 20mins. from now
 file_put_contents("wp-content/themes/World-of-Warcraft-New/includes/stat.json",json_encode($stat));//<--writes new values to status file
 }
 } 
} 
//erorr catchers
// for general errors
function bad($errno, $errstr, $errfile, $errline) {
$stat = json_decode(file_get_contents('wp-content/themes/World-of-Warcraft-New/includes/stat.json'),true);//<--open updater status file
 if ($stat['heath']=="alive" ){//<-- check status file if there is a error already recorded
 $stat['heath']= "dead";//<-- set status value to disable updater
 file_put_contents("wp-content/themes/World-of-Warcraft-New/includes/stat.json",json_encode($stat));//<-- writes new values to status file
 error_log("Error: [$errno] $errstr $errfile $errline",1,
 "[email protected]","From: [email protected]");//<-- email error report
 }
}
set_error_handler("bad");
// for file read errors that could be cuased by file format changes
function badfile(){
 $stat = json_decode(file_get_contents('wp-content/themes/World-of-Warcraft-New/includes/stat.json'),true);
 if ($stat['heath']=="alive" ){
 $stat['heath']= "dead";
 file_put_contents("wp-content/themes/World-of-Warcraft-New/includes/stat.json",json_encode($stat));
 copy("wp-content/themes/World-of-Warcraft-New/includes/back.json","wp-content/themes/World-of-Warcraft-New/includes/token.json");
 error_log("Error: [8] Bad File, updater stopped.",1,
 "[email protected]","From: [email protected]");
 }
}
?>

This the functions file that handles the updating of the data file and error that happen when the script is running. The updater uses a unix time stamp form "strtotime" to make sure that the updater only connects to remote server every twenty minutes. The time stamp and error or "health" state of the script are stored in a status file. The other function of the updater is to create a back of current data file before it is overwritten with new data. I have have been using this script for a few months now and it seem to work fine but there are tow things I could not get around:

  1. FGC downloads the whole file, which includes the current data plus historical data and could not figure out how to grab only the needed data without breaking the json file. I know that there is the "maxlen" parameter that lets you set how much of the data FGC should read but the size and length of the file changed constantly, so it seem like not an option.

  2. A few times the error log reporter has sent a message that says: "trying to get property of non-object in display.php 5". I have looked over both the display.php and token.json files and have not found a problem in either of the files. The last time this happen was on Dec. 1 but no problems reported since then.

asked Dec 9, 2015 at 11:18
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

The json download is likely all or nothing. Partial content couldn't be parsed by the client. The error message may be for line 5:

if(!isset($data, $data->buy,$data->{'24min'},$data->{'24max'},$data->timeToSell,$data->updated)){ //<-- check each object property for data

The object couldn't be parsed it sounds like.

answered Jan 6, 2016 at 14:51
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.