Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/76.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_Html_Simple Html Dom - Fatal编程技术网

Php 我怎样才能加快速度?

Php 我怎样才能加快速度?,php,html,simple-html-dom,Php,Html,Simple Html Dom,我有一个脚本,我认为这是相当基本的刮,叫它什么你会,但它平均需要至少6秒…有可能加快它吗?$date变量仅用于对代码进行计时,并不增加任何重要的时间。我已经设置了两个计时标记,每个标记之间的时间间隔约为3秒。下面的示例URL用于测试 $date = date('m/d/Y h:i:s a', time()); echo "start of timing $date<br /><br />"; include('simple_html_dom.php'); func

我有一个脚本,我认为这是相当基本的刮,叫它什么你会,但它平均需要至少6秒…有可能加快它吗?$date变量仅用于对代码进行计时,并不增加任何重要的时间。我已经设置了两个计时标记,每个标记之间的时间间隔约为3秒。下面的示例URL用于测试

$date = date('m/d/Y h:i:s a', time());

echo "start of timing $date<br /><br />"; 

include('simple_html_dom.php');

function getUrlAddress()
{
$url = $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';
return $url .'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
}

$date = date('m/d/Y h:i:s a', time());  echo "<br /><br />after geturl $date<br /><br />";

$parts = explode("/",$url);

$html = file_get_html($url);

$date = date('m/d/Y h:i:s a', time());  echo "<br /><br />after file_get_url $date<br /><br />";

$file_string = file_get_contents($url);
preg_match('/<title>(.*)<\/title>/i', $file_string, $title);
$title_out = $title[1];

foreach($html->find('img') as $e){

    $image = $e->src;

    if (preg_match("/orangeBlue/", $image)) { $image = ''; }

    if (preg_match("/BeaconSprite/", $image)) { $image = ''; }

    if($image != ''){

    if (preg_match("/http/", $image)) { $image = $image; }

    elseif (preg_match("*//*", $image)) { $image = 'http:'.$image; }

    else { $image = $parts['0']."//".$parts[1].$parts[2]."/".$image; }

    $size = getimagesize($image);
    if (($size[0]>110)&&($size[1]>110)){
    if (preg_match("/http/", $image)) { $image = $image; }
    echo '<img src='.$image.'><br>';
    }
    }
    }

$date = date('m/d/Y h:i:s a', time());  echo "<br /><br />end of timing $date<br /><br />";

它可能是getimagesize函数——它正在运行并获取页面上的每个图像,以便确定大小。也许您可以使用curl编写一些内容,以仅获取内容大小的标题(不过,实际上,这可能是getimagesize所做的)


不管怎么说,在那天我写了一些spider,它的速度有点慢,互联网的速度比以往任何时候都快,它仍然是每个元素的一个抓取。我甚至都不关心图像。

我不是一个PHP爱好者,但在我看来,你要去网络上两次获取文件

首先使用这个:

$html = file_get_html($url);
$file_string = file_get_contents($url);
然后再次使用这个:

$html = file_get_html($url);
$file_string = file_get_contents($url);
因此,如果每次点击都需要几秒钟的时间,你也许可以通过找到一种方法将其减少到一次网络点击来减少时间


要么那样,要么我瞎了。这是一个真正的可能性

您在哪里调用
getUrlAddress()
和/或设置
$url
变量?您有3个计时标记。你能展示他们的输出吗?所以,“计时开始”、“geturl之后”、“file\u get\u url之后”、“计时结束”最突出的一点是您对
preg\u match
的自由使用。有时
strpos
strstr
可能更快。正如@Hans所说,
getImageSize()
也是需要考虑的问题。是的,
getImageSize()
是问题的一部分。我有一个脚本在做类似的事情,我在函数方面也遇到了同样的问题