Frage

I'm trying to add a cache mechanism to my Disqus API function:

<?php
    ini_set('display_errors', 'on');
    $key="MY_PUBLIC_KEY";
    $forum="MY_FORUM";
    $limit = '5';

    $endpoint = 'http://disqus.com/api/3.0/posts/list.json?api_key='.urlencode($key).'&forum='.$forum.'&limit='.$limit.'&related=thread';
    $cache_disqus = '/cache_path/file.json';

    $j=0;
    listposts($endpoint,$j);

    function listposts($endpoint,$j) {

        // Standard CURL
        $session = curl_init($endpoint);
        curl_setopt($session, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($session);
        curl_close($session);

        if(file_exists($cache_disqus) && filemtime($cache_disqus) > time() - 1000){
            // If a cache file newer than 1000 seconds exists, use it
            $results = json_decode($cache_disqus);
        } else {
            $results = json_decode($data);
            file_put_contents($cache_disqus,json_encode($data));
        }
    }
?>

Of course /cache_path/ has permissions properly set.

I get Warning: file_put_contents(): Filename cannot be empty in MY_SCRIPT_FILENAME.php.

War es hilfreich?

Lösung

$cache_disqus is defined outside your function, and is therefore not accessible within the function.

Check out the PHP documentation on variable scope.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top