简体   繁体   中英

Code Slows Website Down, possible error?

This code works, however for unknown reasons, my website loads in 1 second, and when this php is added, it takes 24 seconds. I am still trying to workout why its slowing it down so much, i understand the more lines of code you have the slower things can be by miller seconds, but this is 24 seconds, and looking to fix this even though the script works, I've check with server and nothing technical wrong, its when this code is added, therefore need to correct why this is happening and speed this code up.

<?
class shareCount
{
    private $url, $timeout;
    function __construct($url, $timeout = 10)
    {
        $this->url     = rawurlencode($url);
        $this->timeout = $timeout;
    }

    function get_tweets()
    {
        $json_string = $this->file_get_contents_curl('http://urls.api.twitter.com/1/urls/count.json?url=' . $this->url);
        $json        = json_decode($json_string, true);
        return isset($json['count']) ? intval($json['count']) : 0;
    }

    function get_fb()
    {
        $json_string = $this->file_get_contents_curl('http://api.facebook.com/restserver.php?method=links.getStats&format=json&urls=' . $this->url);
        $json        = json_decode($json_string, true);
        return isset($json[0]['total_count']) ? intval($json[0]['total_count']) : 0;
    }

    function get_plusones()
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, "https://clients6.google.com/rpc");
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_POSTFIELDS, '[{"method":"pos.plusones.get","id":"p","params":{"nolog":true,"id":"' . rawurldecode($this->url) . '","source":"widget","userId":"@viewer","groupId":"@self"},"jsonrpc":"2.0","key":"p","apiVersion":"v1"}]');
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            'Content-type: application/json'
        ));
        $curl_results = curl_exec($curl);
        curl_close($curl);
        $json = json_decode($curl_results, true);
        return isset($json[0]['result']['metadata']['globalCounts']['count']) ? intval($json[0]['result']['metadata']['globalCounts']['count']) : 0;
    }

    private function file_get_contents_curl($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
        curl_setopt($ch, CURLOPT_FAILONERROR, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
        $cont = curl_exec($ch);
        if (curl_error($ch)) {
            die(curl_error($ch));
        }
        return $cont;
    }
}
?>

<div class="entry-share-buttons">
    <div class="share share_size_large share_type_facebook"><span class="share__count"><?php echo $obj->get_fb(); ?></span><a class="share__btn" href="http://www.facebook.com/sharer/sharer.php?u=<?php the_permalink()?>" data-url="<?php the_permalink()?>" data-text="<?php the_title() ?>" target="_blank">Like</a></div>
    <div class="share share_size_large share_type_twitter"><span class="share__count"><?php echo $obj->get_tweets(); ?></span><a class="share__btn" href="http://www.twitter.com/intent/tweet?url=<?php the_permalink()?>" data-url="<?php the_permalink()?>" data-text="<?php the_title() ?>" target="_blank">Tweet</a></div>
    <div class="share share_size_large share_type_email"><span class="share__count"><?php echo $obj->get_plusones(); ?></span><a class="share__btn" href="http://plus.google.com/share?url=<?php the_permalink()?>" data-url="<?php the_permalink()?>" data-text="<?php the_title() ?>" target="_blank">Email</a></div>
    <div class="share share_size_large share_type_comment"><span class="share__count"><?php comments_popup_link( __( '0' ), __( '1' ), __( '%' ) ); ?></span><a class="share__btn" href="#">Comment</a></div>
</div>

I have ran into this same issue recently. The page had many links to download like/share count data from FB and Twitter and page speed decreased dramatically.

The best way I found to solve this issue was to create a table inside a database (if you prefer save it as flat file xml/json that u can update that works too) with the parameters for the information that needs to be refreshed (url/urls).

Write a separate application that runs either as a service or as a scheduled task that you can put a trigger on and set the amount of refreshing you need and thats that.

Real simple, basically you take the load off of the client and put it on the server.

The problem lays with the external requests to Twitter, Facebook and Google which are terribly slow. Nothing you can do about that.

What you can do is cache the amount of likes, shares and +. So that the next visitor doesn't have to wait for the slow API's of Twitter, Facebook and Google.

It is because you are making numerous connection to other websites and grabbing data from it. There is not many ways to simplify this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM