file_put_contents():文件名不能为空 [英] file_put_contents(): Filename cannot be empty
本文介绍了file_put_contents():文件名不能为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将缓存机制添加到我的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
.
解决方案
$cache_disqus
是在您的函数的外部中定义的,因此无法在该函数中访问.
查看有关变量范围的PHP文档. /p>
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
.
解决方案
$cache_disqus
is defined outside your function, and is therefore not accessible within the function.
Check out the PHP documentation on variable scope.
这篇关于file_put_contents():文件名不能为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文