新学习static function为什么返回数据为空?
最近在学习static function,构建一个这样的代码。CURL获取网页源码,正则取出title等,为什么我的代码什么也没有返回?哪里的问题?能否详细解释一下?谢谢。
$url = 'www.php.net/archive/2012.php';
if (!$url){
exit;
}
echo TestClass::getTitle($url);
class TestClass
{
static function getTitle($url)
{
$text = self::getHtml($url);
preg_match("/(<h1.*>)(.*)(<\/h1>)/is",$text,$h1tags);
$title = $h1tags[0];
if (!$title) return false;
return $title;
}
static function getHtml($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
$htmls = curl_exec($ch);
curl_close($ch);
if (!$htmls) return false;
return $htmls;
}
}
$url = 'www.php.net/archive/2012.php';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$htmls = curl_exec($ch);
curl_close($ch);
echo $htmls;