【发布时间】:2017-04-02 01:51:36
【问题描述】:
我的 WordPress 插件中有几行代码,它们应该首先从某个用户那里获取推文,然后返回接收到的 ID 的 oEmbed 代码。虽然这个插件只有几行代码,但是却将我的页面加载时间延长了500-700ms。有什么办法可以改进这个,或者要么捕获结果,所以它不会在每次刷新时运行,因为插件是在页脚中加载的。
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>';
}
}
}
【问题讨论】:
标签: php wordpress rest twitter