【问题标题】:file_put_contents(): Filename cannot be emptyfile_put_contents():文件名不能为空
【发布时间】:2014-06-19 13:27:54
【问题描述】:

我正在尝试向我的 Disqus API 函数添加缓存机制:

<?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));
        }
    }
?>

当然 /cache_path/ 已正确设置权限。

我收到Warning: file_put_contents(): Filename cannot be empty in MY_SCRIPT_FILENAME.php

【问题讨论】:

  • listposts() 函数中的变量范围

标签: php caching file-get-contents json


【解决方案1】:

$cache_disqus 定义在您的函数外部,因此无法在函数内访问。

查看PHP documentation on variable scope

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-29
    • 1970-01-01
    • 2017-06-15
    • 1970-01-01
    • 2012-05-26
    • 2013-03-19
    • 2021-07-26
    相关资源
    最近更新 更多