Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
264 views
in Technique[技术] by (71.8m points)

php - file_put_contents(): Filename cannot be empty

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

Check out the PHP documentation on variable scope.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...