Cain Nuke
Cain Nuke

Reputation: 3083

Get the content of title tag from a webpage with PHP

Hi,

I am trying to get the content fo the title tag from a page within my site. However, file_get_contents is disabled so seems like cURL is my only option. This is what I am trying:

$domain="http://example.com";
ob_start();
$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_URL, $domain. '/blog/index.php?page=4');
$getit = curl_exec($curl_handle);
curl_close($curl_handle);
ob_end_clean();
preg_match("/<title>(.*)<\/title>/i", $getit, $matches);
$title= $matches[1];

I had to use ob_start and clean because otherwise the called page is embedded onto my final hmtl code which I dont need. I only need to get the tag value and have $title display it but it displays nothing. Whats the problem here?

Thank you.

Upvotes: 0

Views: 60

Answers (1)

Kanishka Panamaldeniya
Kanishka Panamaldeniya

Reputation: 17566

Use

curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);

The final code should be

$domain="http://example.com";
ob_start();
$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_URL, $domain. '/blog/index.php?page=4');
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
$getit = curl_exec($curl_handle);
curl_close($curl_handle);
ob_end_clean();
preg_match("/<title>(.*)<\/title>/i", $getit, $matches);
$title= $matches[1];

Upvotes: 0

Related Questions