2016-05-25 8 views
1

私はTwitterAPIExchangeInstagram-PHP-APIを使用して、サーバー上の両方のAPIからデータを要求し、配列に連結し、これをJSONに組み込みました。 jQuery。キャッシュデータサーバー側のAjaxリクエスト

これはすべて正常に動作しますが、非常に遅いです。静的なJSONデータをテストとしてリクエストすると、はるかに高速です。私のサーバ上のPHPスクリプトは毎回実行されているようです(空の配列から始め、ライブラリが要求を行うために提供するラッパーを使用しています)。私がしたいのは、このデータをキャッシュ/ストアし、1日に1〜2回だけ別のリクエストを行うことです。

私の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; 

?> 

私はそうのように私のフロントエンドにそれを持って来る:理にかなっている

$.ajax({ 
type: 'GET', 
    url: 'path/to/script.php' 
    dataType: 'json', 
}).done(function(data) { 
    // do stuff with data here 
    }); 
} 

希望、感謝

+1

それは 'ますfile_put_contents(「cache.txt」、$データ)'あなたは結果をキャッシュすると、その後上部に現在の時刻を確認だろうと最後の時間を使ってファイルを比較置くのと同じくらい簡単です変更されました。何らかの 'if((time() - filemtime( 'cache.txt'))> 24 * 60 * 60){echo file_get_contents( 'cache.txt'); exit(); } '。これは、24時間以上経過していてもデータを再設定したい場合です。 –

+0

有効なキャッシュファイルが存在する場合、apiリクエストはスキップされるように、 'exit()'の使用に注意してください。 –

+0

ニース!ありがとう。私はこれがあまりにもアヤックスの要求をスピードアップしていないことに気づいた...クライアントのために何かのためにそれをキャッシュする必要がありますね –

答えて

1

これが最も簡単ですあなたのコードにキャッシュを追加する方法。

file_put_contents('cache.txt',$data)を使用してリクエストデータをキャッシュし、ファイルの先頭に現在の時刻をチェックし、ファイルが最後に変更された時刻と比較します。 24時間前に変更された場合は、echo file_get_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; 

?> 
+0

^もう一度試してみましたが、それははるかに優れています。愚かなことに、リクエストが行われた後、私はifステートメントを持っていました。乾杯。 –