Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 代码使网站速度变慢,可能出现错误?_Php_Wordpress_Web_Share_Slowdown - Fatal编程技术网

Php 代码使网站速度变慢,可能出现错误?

Php 代码使网站速度变慢,可能出现错误?,php,wordpress,web,share,slowdown,Php,Wordpress,Web,Share,Slowdown,这段代码可以工作,但是由于未知的原因,我的网站在1秒内加载,当添加这个php时,需要24秒。我仍在试图弄明白为什么它会减慢速度如此之快,我理解你的代码行数越多,速度就越慢,但这是24秒,即使脚本可以工作,我也希望解决这个问题,我已经与服务器进行了检查,没有技术问题,当添加此代码时,因此,我们需要纠正为什么会发生这种情况,并加快代码的速度 <? class shareCount { private $url, $timeout; function __construct($u

这段代码可以工作,但是由于未知的原因,我的网站在1秒内加载,当添加这个php时,需要24秒。我仍在试图弄明白为什么它会减慢速度如此之快,我理解你的代码行数越多,速度就越慢,但这是24秒,即使脚本可以工作,我也希望解决这个问题,我已经与服务器进行了检查,没有技术问题,当添加此代码时,因此,我们需要纠正为什么会发生这种情况,并加快代码的速度

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

问题在于对Twitter、Facebook和谷歌的外部请求非常缓慢。对此你无能为力


你能做的就是缓存喜欢、分享和+的数量。这样,下一个访问者就不必等待Twitter、Facebook和谷歌的缓慢API。

这是因为你正在与其他网站建立大量连接并从中获取数据。简化这个问题的方法不多。

我最近也遇到过同样的问题。该页面有许多链接可从FB和Twitter下载like/share count数据,页面速度急剧下降

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

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


非常简单,基本上就是将客户机的负载卸下,放到服务器上。

运行此代码时,您要建立三个curl连接。这些将被一个接一个地制作——试着一个接一个地禁用它们,看看它们是否特别慢;否则,我建议您在每个页面上运行一些缓存,这样您就可以按照您所说的那样运行代码,而Google+则会显著降低速度,虽然这并不令人惊讶,但使用Facebook、Twitter、,Google+已经提供了最佳实践……这也是一个补充。Facebook、Google、Twitter等公司不喜欢脚本总是在没有缓存的情况下命中它们,他们有实现功能来阻止这种情况。谷歌地图示例:他可以修改代码,使其作为cronjob运行,并更新数字。面向客户的代码从数据库中读取数字,而cronjob每隔一段时间运行一次并更新这些数字。这将从客户端删除运行缓慢的代码。@andrewsi请您告诉我这一点,因为这是我一整天都在绞尽脑汁的事情:/@user3348182-简而言之,您需要做的是在您的问题中获取代码,并对其进行调整。与其打印函数的结果,不如将其保存到数据库中。这就是该程序需要做的全部工作——创建curl连接并保存输出。面向客户的代码只是从表中选择值,并显示这些值。您可以使用cron每隔一段时间(比如每小时)运行第一个代码。这有帮助吗?