file_put_contents(): Filename cannot be empty

你。 提交于 2019-12-13 08:15:11

问题


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.


回答1:


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

Check out the PHP documentation on variable scope.



来源:https://stackoverflow.com/questions/23435307/file-put-contents-filename-cannot-be-empty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!