我是学习新手,php static function
我想构建一个函数,从某个 url 卷曲一些内容,然后处理 php regex 以获得我需要的东西。这是我的代码,但 curl 部分运行了两次。如何修改它以缩短运行时间?
$url = 'www.php.net/archive/2012.php';
if (!$url){
exit;
}
echo TestClass::getTitle1($url);
echo '<hr />';
echo TestClass::getTitle2($url);
class TestClass
{
static function getTitle1($url)
{
$data = self::getHtml($url);// run in first time
preg_match("/(<h1.*>)(.*)(<\/h1>)/",$data,$h1tags);
$h1 = $h1tags[0];
if (!$h1) return false;
return $h1;
}
static function getTitle2($url)
{
$data = self::getHtml($url);// run in second time
preg_match("/(<h2.*>)(.*)(<\/h2>)/",$data,$h2tags);
$h2 = $h2tags[0];
if (!$h2) return false;
return $h2;
}
static function getHtml($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$htmls = curl_exec($ch);
curl_close($ch);
if (!$htmls) return false;
return $htmls;
}
}