Skip to content

Instantly share code, notes, and snippets.

@nagarjun
Created November 10, 2013 09:20
Show Gist options
  • Save nagarjun/7395910 to your computer and use it in GitHub Desktop.
Save nagarjun/7395910 to your computer and use it in GitHub Desktop.
Get the Title by passing the URL using PHP.
/**
* Get the contents of Title tag from URL.
*/
function get_title($URL)
{
// Set the user agent so external servers don't block us.
ini_set('user_agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.11) Gecko/2009060215 Firefox/3.0.11');
$page_contents = file_get_contents($URL);
$dom = new DOMDocument();
@$dom->loadHTML($page_contents);
$title = $dom->getElementsByTagName('title');
return $title->item(0)->nodeValue;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment