New Learning static function Why is the returned data empty?
Recently in learning the static function, build a code like this. Curl Gets the Web page source code, take out the title and so on, why does my code return nothing? Where's the problem? Can you explain it in detail? Thank you.
$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>)/is", $text, $h 1tags);
$title = $h 1tags[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;
}
}
------Optimal Solution--------------------
That's your regular string of mistakes.
/( ) (. *) (<\/h1>)/is
------Other Solutions--------------------
Initializes a new object, instantiates
------Other Solutions--------------------
This post was last edited by xuzuning on 2012-11-10 15:32:54 your code is not able to display the entire page?
Of course, to make your code run as you expect, you also need to
curl_setopt ($ch, Curlopt_returntransfer, true);
In $htmls = Curl_exec ($ch); Before
------Other Solutions--------------------
Citation:
This post was last edited by xuzuning on 2012-11-10 15:32:54
Isn't it possible to run your code to show the entire page?
Of course, to make your code run as you expect, you also need to
curl_setopt ($ch, Curlopt_returntransfer, true);
In $htmls = Curl_exec ($ch); Before
Set, or output blank, nothing, my php Version 5.3.8.
I want to go back to the regular out
the text part.
------Other Solution--------------------
reference:
First new object, real To instantiate a
How about a new method? No.
------Other Solutions--------------------
So you're probably not loading curl extensions?
------Other Solutions--------------------
reference:
that you might not be loading Curl extension? The
Simple Curl code can return the contents of the www.php.net. But build a static function, but nothing.
$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;
------Other Solutions--------------------
reference:
That's your regular rule. The string is incorrectly
/( ) (. *) (<\/h1>)/is
Does it make sense to remove the IS?