【问题标题】:GET Requests in Wordpress Plugin extend the page load time for approx. 500-700msWordpress 插件中的 GET 请求将页面加载时间延长了大约。 500-700毫秒
【发布时间】: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


    【解决方案1】:

    您应该使用WordPress Transients API 来存储响应。您可以检查几条推文之间的时间距离,并将其用于瞬时过期时间。 当然你可以保存 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>';
    
        }
    }
    

    【讨论】:

    • 帮了很多忙,不知道Transients API。干杯伙伴。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-16
    • 1970-01-01
    相关资源
    最近更新 更多