Help with D3 toon cache php

85 Night Elf Druid
0
Hey all, I'm trying to set up some cacheing for players when i pull them (testing with local .json files currently.) However, the cacheing isn't working and is AWLAYS updating no matter if the last-modified are the same. Was wondering if anyone could help me out and take a look on it.


public function pullData()
{
switch ($this->pull):

case "career":
$this->URL = "/api/d3/account/" . $this->name . "-" . $this->tag . ".json";
$profile = $this->region . "." . $this->name . "-" . $this->tag . ".json";
break;

case "hero":
$this->URL = "/api/d3/account/" . $this->name . "-" . $this->tag . "/hero/" . $this->id . ".json";
$profile = $this->region . "." . $this->name . "-" .$this->tag . "." .$this->id . ".json";
break;

endswitch;

$this->URL = "http://". $this->host . $this->URL;
$profile = strtolower($profile);
$cache_filename = dirname(__FILE__) . "/cache/characters/" . $profile;
$lastModified = 0;

if(file_exists($cache_filename)) {
$contents = file_get_contents($cache_filename);
$json = json_decode($contents);
$lastModified = ((int)$json->last-updated / 1000);
}

$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $this->URL );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 15 );
curl_setopt( $ch, CURLOPT_USERAGENT, self::BROWSER );
curl_setopt( $ch, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE );
curl_setopt( $ch, CURLOPT_TIMEVALUE, $lastModified );
$curl_return = curl_exec( $ch );
curl_close( $ch );

if ( $curl_return ) {
$handle = fopen( $cache_filename, "w" );
fwrite( $handle, $curl_return );
fclose( $handle );
}

$contents = file_get_contents( $cache_filename );
return json_decode( $contents );
}


I basically reused a WOW API cache that i use and replaced it with the D3 information, but it's not working. Wondering if anyone else has had trouble.
Reply Quote
95 Pandaren Warrior
15445
There is never any comparison between cached and fetched files?


$lastModified = 0;
$lastModified = ((int)$json->last-updated / 1000);
curl_setopt( $ch, CURLOPT_TIMEVALUE, $lastModified );


You 'initialize' $lastModified
Then set it to the cached value
Then set it to the remote value
But never compare the values and since $curl_return will always be non-zero you're going to overwrite it in all cases

Something like should work


curl_setopt( $ch, CURLOPT_TIMEVALUE, $remoteLastModified );
$curl_return = curl_exec( $ch );
curl_close( $ch );

if ( $remoteLastModified > $lastModified ) {
$handle = fopen( $cache_filename, "w" );
fwrite( $handle, $curl_return );
fclose( $handle );
}
Reply Quote
85 Night Elf Druid
0
well played, can't believe i was missing that. Thanks!
Reply Quote
85 Night Elf Druid
0
Allrighty, figured it out, just incase anyone else was trying their API. It had nothing to do with the comparison, as the TIMEVALUE and COND do that for me, it was the fact that I had the item hosted locally, and the real time since mod wasn't the same as the last-updated. Once I updated the last-updated on my cache files to real time it worked fine.
Reply Quote

Please report any Code of Conduct violations, including:

Threats of violence. We take these seriously and will alert the proper authorities.

Posts containing personal information about other players. This includes physical addresses, e-mail addresses, phone numbers, and inappropriate photos and/or videos.

Harassing or discriminatory language. This will not be tolerated.

Forums Code of Conduct

Report Post # written by

Reason
Explain (256 characters max)
Submit Cancel

Reported!

[Close]