Php 在Wordpress插件中获取请求可将页面加载时间延长约500-700毫秒
我的WordPress插件中有几行代码,应该首先从某个用户那里获取推文,然后返回接收到的ID的嵌入代码。虽然这个插件只有几行代码,但它将我的页面加载时间延长了500-700毫秒。有没有什么方法可以改进这一点,或者捕获结果,这样它就不会在每次刷新时运行,因为插件加载在页脚中Php 在Wordpress插件中获取请求可将页面加载时间延长约500-700毫秒,php,wordpress,rest,twitter,Php,Wordpress,Rest,Twitter,我的WordPress插件中有几行代码,应该首先从某个用户那里获取推文,然后返回接收到的ID的嵌入代码。虽然这个插件只有几行代码,但它将我的页面加载时间延长了500-700毫秒。有没有什么方法可以改进这一点,或者捕获结果,这样它就不会在每次刷新时运行,因为插件加载在页脚中 class TwitterFeed { public static function displayTweets( $count = 1) { $settings = array(
class TwitterFeed {
public static function displayTweets( $count = 1) {
$settings = array(
'oauth_access_token' => "",
'oauth_access_token_secret' => "",
'consumer_key' => "",
'consumer_secret' => ""
);
// Searches Tweets from the User
$url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$getfield = '?screen_name=ipmPress&count=' . $count . '';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$tweets = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
$response = json_decode($tweets);
// Gets Embed Response for the Collected Tweets
foreach ($response as $tweet) {
$url = 'https://api.twitter.com/1.1/statuses/oembed.json';
$getfield = '?id=' . $tweet->id . '&omit_script=true&hide_media=true';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$tweets = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
$response = json_decode($tweets);
echo
'<div class="large-4 medium-6 column">' .
$response->html .
'</div>';
}
}
}
类TwitterFeed{
公共静态函数displayteets($count=1){
$settings=数组(
“oauth\u访问\u令牌”=>“”,
“oauth\u访问\u令牌\u秘密”=>“”,
“消费者密钥”=>“”,
“消费者机密”=>“”
);
//搜索用户的推文
$url='1https://api.twitter.com/1.1/statuses/user_timeline.json';
$getfield='?屏幕名称=ipmPress&count='。$count';
$requestMethod='GET';
$twitter=newtwitterapiexchange($settings);
$tweets=$twitter->setGetfield($getfield)
->buildOauth($url,$requestMethod)
->performRequest();
$response=json_decode($tweets);
//获取收集的tweet的嵌入响应
foreach($tweet响应){
$url='1https://api.twitter.com/1.1/statuses/oembed.json';
$getfield='?id='。$tweet->id.&omit_script=true&hide_media=true';
$requestMethod='GET';
$twitter=newtwitterapiexchange($settings);
$tweets=$twitter->setGetfield($getfield)
->buildOauth($url,$requestMethod)
->performRequest();
$response=json_decode($tweets);
回声
'' .
$response->html。
'';
}
}
}
您应该使用来存储响应。您可以检查几条tweet之间的时间距离,并将其用于短暂过期时间。
当然,您可以将提要保存12小时,但不建议这样做,我每30分钟提出一次新请求,这是正常的。下面是使用您的代码的示例:
public static function displayTweets( $count = 1) {
$key = "twitter-transient";
$cached = get_transient($key);
if (!$cached) {
$settings = array(
'oauth_access_token' => "",
'oauth_access_token_secret' => "",
'consumer_key' => "",
'consumer_secret' => ""
);
// Searches Tweets from the User
$url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$getfield = '?screen_name=ipmPress&count=' . $count . '';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$tweets = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
$response = json_decode($tweets);
set_transient($key, $response, 1800);
$cached = $response;
}
// Gets Embed Response for the Collected Tweets
foreach ($cached as $tweet) {
$url = 'https://api.twitter.com/1.1/statuses/oembed.json';
$getfield = '?id=' . $tweet->id . '&omit_script=true&hide_media=true';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$tweets = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
$response = json_decode($tweets);
echo
'<div class="large-4 medium-6 column">' .
$response->html .
'</div>';
}
}
public静态函数显示tweets($count=1){
$key=“twitter瞬态”;
$cached=get_transient($key);
如果(!$cached){
$settings=数组(
“oauth\u访问\u令牌”=>“”,
“oauth\u访问\u令牌\u秘密”=>“”,
“消费者密钥”=>“”,
“消费者机密”=>“”
);
//搜索用户的推文
$url='1https://api.twitter.com/1.1/statuses/user_timeline.json';
$getfield='?屏幕名称=ipmPress&count='。$count';
$requestMethod='GET';
$twitter=newtwitterapiexchange($settings);
$tweets=$twitter->setGetfield($getfield)
->buildOauth($url,$requestMethod)
->performRequest();
$response=json_decode($tweets);
设置瞬态($key$response,1800);
$cached=$response;
}
//获取收集的tweet的嵌入响应
foreach($缓存为$tweet){
$url='1https://api.twitter.com/1.1/statuses/oembed.json';
$getfield='?id='。$tweet->id.&omit_script=true&hide_media=true';
$requestMethod='GET';
$twitter=newtwitterapiexchange($settings);
$tweets=$twitter->setGetfield($getfield)
->buildOauth($url,$requestMethod)
->performRequest();
$response=json_decode($tweets);
回声
'' .
$response->html。
'';
}
}
您应该使用来存储响应。您可以检查几条tweet之间的时间距离,并将其用于短暂过期时间。
当然,您可以将提要保存12小时,但不建议这样做,我每30分钟提出一次新请求,这是正常的。下面是使用您的代码的示例:
public static function displayTweets( $count = 1) {
$key = "twitter-transient";
$cached = get_transient($key);
if (!$cached) {
$settings = array(
'oauth_access_token' => "",
'oauth_access_token_secret' => "",
'consumer_key' => "",
'consumer_secret' => ""
);
// Searches Tweets from the User
$url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$getfield = '?screen_name=ipmPress&count=' . $count . '';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$tweets = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
$response = json_decode($tweets);
set_transient($key, $response, 1800);
$cached = $response;
}
// Gets Embed Response for the Collected Tweets
foreach ($cached as $tweet) {
$url = 'https://api.twitter.com/1.1/statuses/oembed.json';
$getfield = '?id=' . $tweet->id . '&omit_script=true&hide_media=true';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$tweets = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
$response = json_decode($tweets);
echo
'<div class="large-4 medium-6 column">' .
$response->html .
'</div>';
}
}
public静态函数显示tweets($count=1){
$key=“twitter瞬态”;
$cached=get_transient($key);
如果(!$cached){
$settings=数组(
“oauth\u访问\u令牌”=>“”,
“oauth\u访问\u令牌\u秘密”=>“”,
“消费者密钥”=>“”,
“消费者机密”=>“”
);
//搜索用户的推文
$url='1https://api.twitter.com/1.1/statuses/user_timeline.json';
$getfield='?屏幕名称=ipmPress&count='。$count';
$requestMethod='GET';
$twitter=newtwitterapiexchange($settings);
$tweets=$twitter->setGetfield($getfield)
->buildOauth($url,$requestMethod)
->performRequest();
$response=json_decode($tweets);
设置瞬态($key$response,1800);
$cached=$response;
}
//获取收集的tweet的嵌入响应
foreach($缓存为$tweet){
$url='1https://api.twitter.com/1.1/statuses/oembed.json';
$getfield='?id='。$tweet->id.&omit_script=true&hide_media=true';
$requestMethod='GET';
$twitter=newtwitterapiexchange($settings);
$tweets=$twitter->setGetfield($getfield)
->buildOauth($url,$requestMethod)
->performRequest();
$response=json_decode($tweets);
回声
'' .
$response->html。
'';
}
}
帮助很大,不知道瞬态API。干杯,伙计。帮了很多忙,不知道瞬变API。干杯,伙计。