Code减慢网站速度,可能出错?

时间:2014-08-05 18:57:33

标签: php wordpress web share slowdown

此代码有效,但由于未知原因,我的网站加载时间为1秒,当添加此php时,需要24秒。我仍然在努力锻炼为什么它减慢了这么多,我明白你的代码行数越多,米勒秒越慢,但这是24秒,即使脚本有效,我也希望解决这个问题。 #39; ve检查服务器并没有任何技术错误,它在添加此代码时,因此需要纠正这种情况发生的原因并加快此代码的速度。

<?
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>

3 个答案:

答案 0 :(得分:1)

我最近遇到过同样的问题。该页面有许多链接可以从FB和Twitter下载/共享计数数据,并且页面速度显着下降。

我发现解决此问题的最佳方法是在数据库中创建一个表(如果您希望将其保存为平面文件xml / json,您可以更新它也可以更新),并使用需要的信息参数刷新(url / urls)。

编写一个单独的应用程序,既可以作为服务运行,也可以作为计划任务运行,您可以设置触发器并设置所需的刷新量等等。

非常简单,基本上你将客户端的负载关闭并将其放在服务器上。

答案 1 :(得分:0)

问题在于对Twitter,Facebook和Google的外部请求非常缓慢。你无能为力。

你可以做的是缓存喜欢,分享和+的数量。因此,下一位访问者不必等待Twitter,Facebook和Google的API缓慢。

答案 2 :(得分:0)

这是因为您与其他网站建立了大量连接并从中获取数据。简化这一点的方法并不多。