Mai*_*ube 6 php performance curl codeigniter
最近我用Curl将我的抓取代码移动到了CodeIgniter.我正在使用来自http://philsturgeon.co.uk/code/codeigniter-curl的 Curl CI库.我把抓取过程放在一个控制器中,然后我发现我的抓取的执行时间比我在普通PHP中构建的执行时间要慢.
CodeIgniter输出结果需要12秒,而普通PHP只需要6秒.两者都包括HTML DOM解析器的解析过程.
这是我在CodeIgniter中的Curl代码:
function curl($url, $postdata=false)
{
$agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)";
$this->curl->create($url);
$this->curl->ssl(false);
$options = array(
'URL' => $url,
'HEADER' => 0,
'AUTOREFERER' => true,
'FOLLOWLOCATION' => true,
'TIMEOUT' => 60,
'RETURNTRANSFER' => 1,
'USERAGENT' => $agent,
'COOKIEJAR' => dirname(__FILE__) . "/cookie.txt",
'COOKIEFILE' => dirname(__FILE__) . "/cookie.txt",
);
if($postdata)
{
$this->curl->post($postdata, $options);
}
else
{
$this->curl->options($options);
}
return $this->curl->execute();
}
Run Code Online (Sandbox Code Playgroud)
非codeigniter(纯PHP)代码:
function curl($ url,$ binary = false,$ post = false,$ cookie = false){
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Accepts all CAs
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt ($ch, CURLOPT_URL, $url );
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
if($cookie){
$agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)";
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__) . "/cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__) . "/cookie.txt");
}
if($binary)
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
if($post){
foreach($post as $key=>$value)
{
$post_array_string1 .= $key.'='.$value.'&';
}
$post_array_string1 = rtrim($post_array_string1,'&');
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_array_string1);
}
return curl_exec ($ch);
Run Code Online (Sandbox Code Playgroud)
}
有谁知道为什么这个CodeIgniter Curl会慢一些?或者是因为simple_html_dom解析器?
我必须更多地了解 CI 库,以及它是否对收集的数据执行任何额外的任务,但我会尝试将您的方法命名为库名称以外的名称。我遇到了 Facebook 库的问题,在名为 facebook 的方法中调用它会导致问题。如果您谈论的是库或方法, $this->curl 可能会含糊不清。
另外,尝试添加调试分析器并查看它会产生什么结果。在构造或方法中添加以下内容:
$this->output->enable_profiler(TRUE);
Run Code Online (Sandbox Code Playgroud)