Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.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
Php 缓存ajax请求的数据服务器端_Php_Jquery_Json_Ajax_Caching - Fatal编程技术网

Php 缓存ajax请求的数据服务器端

Php 缓存ajax请求的数据服务器端,php,jquery,json,ajax,caching,Php,Jquery,Json,Ajax,Caching,我使用and从服务器上的两个API请求数据,将它们连接到一个数组中,并将其构建到JSON中,我可以在客户端通过jQuery中的一个简单GET请求使用它 这一切都很好,但速度相当慢。当我请求静态JSON数据作为测试时,速度要快得多。我的服务器上获取此数据的php脚本似乎每次都在运行(因此从一个空数组开始,使用库提供的包装器进行请求等)。我想做的是缓存/存储这些数据,并且每天只发出一次或两次另一个请求 我的PHP知识不是最好的,所以我相信有一种相对简单的方法可以做到这一点 以下是我的服务器上的php

我使用and从服务器上的两个API请求数据,将它们连接到一个数组中,并将其构建到JSON中,我可以在客户端通过jQuery中的一个简单GET请求使用它

这一切都很好,但速度相当慢。当我请求静态JSON数据作为测试时,速度要快得多。我的服务器上获取此数据的php脚本似乎每次都在运行(因此从一个空数组开始,使用库提供的包装器进行请求等)。我想做的是缓存/存储这些数据,并且每天只发出一次或两次另一个请求

我的PHP知识不是最好的,所以我相信有一种相对简单的方法可以做到这一点

以下是我的服务器上的php脚本:

<?php

header('Content-type:application/json;charset=utf-8');

require_once('TwitterAPIExchange.php');
require_once('Instagram.php');

unset($allFeeds);
$allFeeds = array();

$settings = array(
'oauth_access_token' => "XXXX' => "XXXX",
'consumer_key' => "XXXX",
'consumer_secret' => "XXXX"
);

$url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$getfield = '?screen_name=XXX';
$requestMethod = 'GET';

$twitter = new TwitterAPIExchange($settings);

$twitter_response = $twitter->setGetfield($getfield)->buildOauth($url,$requestMethod)->performRequest();


$jsonTweets = json_decode($twitter_response);

foreach ($jsonTweets as $tweet) {

 $tweetObj = new stdClass();
 $tweetObj->type = 'Twitter';
 $tweetObj->created_at = strtotime($tweet->created_at);
 $tweetObj->text = $tweet->text;
 $allFeeds[] = $tweetObj;
}

use MetzWeb\Instagram\Instagram;

$instagram = new Instagram(array(
 'apiKey'      => 'XXXX',
 'apiSecret'   => 'XXXX',
 'apiCallback' => 'XXXX'
));

$token = 'XXX';
$code = $token;
$instagram->setAccessToken($code);

$id_one = 'XXX';
$id_two = 'XXX';

$insta_response_one = $instagram->getUserMedia($id_one, 20);
$insta_response_two = $instagram->getUserMedia($id_two, 10);

foreach ($insta_response_one->data as $insta_one) {
 $instaObjOne = new stdClass();
 $instaObjOne->type = 'Instagram One';
 $instaObjOne->created_at = (int)$insta_one->created_time;

  if (isset($insta_one->caption->text)) {
   $instaObjOne->text = $insta_one->caption->text;
  }

$instaObjOne->img = $insta_one->images->standard_resolution->url;
$allFeeds[] = $instaObjOne;

}

foreach ($insta_response_two->data as $insta_two) {
 $instaObjTwo = new stdClass();
 $instaObjTwo->type = 'Instagram Two';
 $instaObjTwo->created_at = (int)$insta_two->created_time;
 if (isset($insta_two->caption->text)) {
   $instaObjTwo->text = $insta_two->caption->text;
 }
$instaObjTwo->img = $insta_two->images->standard_resolution->url;
 $allFeeds[] = $instaObjTwo;
}

function dateSort($a, $b) {
 return $b->created_at - $a->created_at;
}

usort($allFeeds, "dateSort");

$data = json_encode($allFeeds);

// cache $data here?

echo $data;

?>

希望这是有意义的,谢谢这是向代码中添加缓存的最简单方法

它使用
file\u put\u contents('cache.txt',$data)
缓存请求数据,然后在文件顶部检查当前时间并与上次修改文件的时间进行比较。如果不到24小时前修改过,它只使用
echo file\u get\u contents('cache.txt)输出缓存文件的内容
并停止脚本

<?php

header('Content-type:application/json;charset=utf-8');

// Check if file was modified less than 24 hours ago
if ((time() - filemtime('cache.txt')) < 24 * 60 * 60) { 

    // Output contents of cache file and stop script
    echo file_get_contents('cache.txt');
    exit(); 
}

require_once('TwitterAPIExchange.php');
require_once('Instagram.php');

unset($allFeeds);
$allFeeds = array();

$settings = array(
'oauth_access_token' => "XXXX" => "XXXX",
'consumer_key' => "XXXX",
'consumer_secret' => "XXXX"
);

$url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$getfield = '?screen_name=XXX';
$requestMethod = 'GET';

$twitter = new TwitterAPIExchange($settings);

$twitter_response = $twitter->setGetfield($getfield)->buildOauth($url,$requestMethod)->performRequest();


$jsonTweets = json_decode($twitter_response);

foreach ($jsonTweets as $tweet) {

 $tweetObj = new stdClass();
 $tweetObj->type = 'Twitter';
 $tweetObj->created_at = strtotime($tweet->created_at);
 $tweetObj->text = $tweet->text;
 $allFeeds[] = $tweetObj;
}

use MetzWeb\Instagram\Instagram;

$instagram = new Instagram(array(
 'apiKey'      => 'XXXX',
 'apiSecret'   => 'XXXX',
 'apiCallback' => 'XXXX'
));

$token = 'XXX';
$code = $token;
$instagram->setAccessToken($code);

$id_one = 'XXX';
$id_two = 'XXX';

$insta_response_one = $instagram->getUserMedia($id_one, 20);
$insta_response_two = $instagram->getUserMedia($id_two, 10);

foreach ($insta_response_one->data as $insta_one) {
 $instaObjOne = new stdClass();
 $instaObjOne->type = 'Instagram One';
 $instaObjOne->created_at = (int)$insta_one->created_time;

  if (isset($insta_one->caption->text)) {
   $instaObjOne->text = $insta_one->caption->text;
  }

$instaObjOne->img = $insta_one->images->standard_resolution->url;
$allFeeds[] = $instaObjOne;

}

foreach ($insta_response_two->data as $insta_two) {
 $instaObjTwo = new stdClass();
 $instaObjTwo->type = 'Instagram Two';
 $instaObjTwo->created_at = (int)$insta_two->created_time;
 if (isset($insta_two->caption->text)) {
   $instaObjTwo->text = $insta_two->caption->text;
 }
$instaObjTwo->img = $insta_two->images->standard_resolution->url;
 $allFeeds[] = $instaObjTwo;
}

function dateSort($a, $b) {
 return $b->created_at - $a->created_at;
}

usort($allFeeds, "dateSort");

$data = json_encode($allFeeds);

// Cache $data here
file_put_contents('cache.txt', $data);

echo $data;

?>


只需将
文件放入内容('cache.txt',$data)
您希望缓存结果的位置,然后在顶部检查当前时间并与上次修改文件的时间进行比较。类似于
if((time()-filemtime('cache.txt'))>24*60*60){echo file_get_contents('cache.txt');exit();}
。如果数据已超过24小时,则需要重新蚀刻数据。请注意
exit()
的用法,因此如果存在有效的缓存文件,则跳过api请求。很好!谢谢我已经注意到这并没有加快ajax请求的速度,但是…我想我必须以某种方式将其缓存到客户机上以实现这一点。。。顺便说一句,我在上面写评论时把事情搞砸了。当您比较文件被修改的时间时,它应该是小于号。不过,我在回答中解决了这个问题。^再试一次,效果明显更好。愚蠢的是,在提出请求后,我得到了if声明。干杯
<?php

header('Content-type:application/json;charset=utf-8');

// Check if file was modified less than 24 hours ago
if ((time() - filemtime('cache.txt')) < 24 * 60 * 60) { 

    // Output contents of cache file and stop script
    echo file_get_contents('cache.txt');
    exit(); 
}

require_once('TwitterAPIExchange.php');
require_once('Instagram.php');

unset($allFeeds);
$allFeeds = array();

$settings = array(
'oauth_access_token' => "XXXX" => "XXXX",
'consumer_key' => "XXXX",
'consumer_secret' => "XXXX"
);

$url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$getfield = '?screen_name=XXX';
$requestMethod = 'GET';

$twitter = new TwitterAPIExchange($settings);

$twitter_response = $twitter->setGetfield($getfield)->buildOauth($url,$requestMethod)->performRequest();


$jsonTweets = json_decode($twitter_response);

foreach ($jsonTweets as $tweet) {

 $tweetObj = new stdClass();
 $tweetObj->type = 'Twitter';
 $tweetObj->created_at = strtotime($tweet->created_at);
 $tweetObj->text = $tweet->text;
 $allFeeds[] = $tweetObj;
}

use MetzWeb\Instagram\Instagram;

$instagram = new Instagram(array(
 'apiKey'      => 'XXXX',
 'apiSecret'   => 'XXXX',
 'apiCallback' => 'XXXX'
));

$token = 'XXX';
$code = $token;
$instagram->setAccessToken($code);

$id_one = 'XXX';
$id_two = 'XXX';

$insta_response_one = $instagram->getUserMedia($id_one, 20);
$insta_response_two = $instagram->getUserMedia($id_two, 10);

foreach ($insta_response_one->data as $insta_one) {
 $instaObjOne = new stdClass();
 $instaObjOne->type = 'Instagram One';
 $instaObjOne->created_at = (int)$insta_one->created_time;

  if (isset($insta_one->caption->text)) {
   $instaObjOne->text = $insta_one->caption->text;
  }

$instaObjOne->img = $insta_one->images->standard_resolution->url;
$allFeeds[] = $instaObjOne;

}

foreach ($insta_response_two->data as $insta_two) {
 $instaObjTwo = new stdClass();
 $instaObjTwo->type = 'Instagram Two';
 $instaObjTwo->created_at = (int)$insta_two->created_time;
 if (isset($insta_two->caption->text)) {
   $instaObjTwo->text = $insta_two->caption->text;
 }
$instaObjTwo->img = $insta_two->images->standard_resolution->url;
 $allFeeds[] = $instaObjTwo;
}

function dateSort($a, $b) {
 return $b->created_at - $a->created_at;
}

usort($allFeeds, "dateSort");

$data = json_encode($allFeeds);

// Cache $data here
file_put_contents('cache.txt', $data);

echo $data;

?>