Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JSON推特feed&;缓存错误_Json_Wordpress - Fatal编程技术网

JSON推特feed&;缓存错误

JSON推特feed&;缓存错误,json,wordpress,Json,Wordpress,我最近编写了一个小脚本,它可以获取我的Twitter提要,并将其缓存以保存在HTTP请求上。如果没有WordPress,它工作得很好,只要我把它添加到WordPress中,一切都会出错,这是有道理的,因为文件内容在WordPress中并不特别正常。我尝试改为使用:wp\u remote\u get(),但出现以下错误: Fatal error: Cannot use object of type WP_Error as array in 这是哪一行: $tweets = json_decode

我最近编写了一个小脚本,它可以获取我的Twitter提要,并将其缓存以保存在HTTP请求上。如果没有WordPress,它工作得很好,只要我把它添加到WordPress中,一切都会出错,这是有道理的,因为文件内容在WordPress中并不特别正常。我尝试改为使用:wp\u remote\u get(),但出现以下错误:

Fatal error: Cannot use object of type WP_Error as array in
这是哪一行:

$tweets = json_decode($contents['body']);
不添加到WordPress的代码:(工作正常)

$file=“tweets.txt”;
如果(@file_存在($file)和@filemtime($file)>=strotime(“-10分钟”)){
$tweets=json_解码(文件获取内容($file));
}否则{
$tweets=文件获取内容(“http://api.twitter.com/1/statuses/user_timeline.json?screen_name=jack&include_rts=true");
$fh=fopen($file,'w');
fwrite($fh,$tweets);
fclose($fh);
$tweets=json_解码($tweets);
}
$i=0;
foreach($tweets作为$tweet){
$tweetText=preg_replace(“#\b([\w-]+:/?| www[.])[^\s()+(?:\([\w\d]+\)|([^[:putt:][\s]./))#,“$tweet->text”);
$tweetText=preg_replace('/(^|\s)@([a-z0-9+)/i','$1',$tweetText);
$tweetText.=“-”.date('G:ifjs',strottime($tweet->created_at));
回显“”.$tweetText.“

”; ++$i; 如果($i==3){break;} }
添加到WordPress时的代码:

$file = "tweets.txt";
if (@file_exists($file) and @filemtime($file)>=strtotime("-10 minutes")) {
    $contents = wp_remote_get($file);
    $tweets = json_decode($contents['body']);
} else {
    $tweets = wp_remote_get("http://api.twitter.com/1/statuses/user_timeline.json?screen_name=jack&include_rts=true");
    $fh = fopen($file, 'w');
    fwrite($fh, $tweets);
    fclose($fh);
    $tweets = json_decode($contents['body']);
}
$i = 0;
foreach($tweets as $tweet) {
    $tweetText = preg_replace('#\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))#','<a href="$1">$1</a>', $tweet->text);
    $tweetText = preg_replace('/(^|\s)@([a-z0-9_]+)/i','$1<a href="http://www.twitter.com/$2">@$2</a>', $tweetText);
    $tweetText .= " - " . date('G:i F jS',strtotime($tweet->created_at));
    echo '<p>'.$tweetText.'</p>';
    ++$i;
    if ($i==3) {break;}
}   
$file=“tweets.txt”;
如果(@file_存在($file)和@filemtime($file)>=strotime(“-10分钟”)){
$contents=wp\u remote\u get($file);
$tweets=json_decode($contents['body']);
}否则{
$tweets=wp\u remote\u get(“http://api.twitter.com/1/statuses/user_timeline.json?screen_name=jack&include_rts=true");
$fh=fopen($file,'w');
fwrite($fh,$tweets);
fclose($fh);
$tweets=json_decode($contents['body']);
}
$i=0;
foreach($tweets作为$tweet){
$tweetText=preg_replace(“#\b([\w-]+:/?| www[.])[^\s()+(?:\([\w\d]+\)|([^[:putt:][\s]./))#,“$tweet->text”);
$tweetText=preg_replace('/(^|\s)@([a-z0-9+)/i','$1',$tweetText);
$tweetText.=“-”.date('G:ifjs',strottime($tweet->created_at));
回显“”.$tweetText.“

”; ++$i; 如果($i==3){break;} }

我对使用像JSON这样的HTTP请求非常陌生。如果我遗漏了什么,并且/或者这成为了一个非常简单的解决方案,我很抱歉。谢谢。

从法典中可以看出,
wp\u remote\u get()
以数组形式返回结果

正如您在这个答案中所看到的-,您需要在
json\u decode()
中指定它是一个数组而不是一个对象

所以我认为应该是这样

$tweets = json_decode($contents['body'], true);

用它来工作,出于某种原因,我认为默认情况下这是真的-我的错。我还必须对tweet进行编码才能将其写入文件。谢谢你的帮助,很好!记住为将来的用户将答案标记为正确;)对不起,我完全忘了!
$tweets = json_decode($contents['body'], true);