Cant get title and meta description from URL?
Posted: Tue Aug 03, 2010 10:44 am
Hi
I am trying to pull title and meta description from an URL..
To pull the URL I am using the code from http://www.dreamincode.net/code/snippet4625.htm...
And to pull the meta description I am using the code from http://php.net/manual/en/function.get-meta-tags.php.
So my complete code looks like this:
But it wont work... All I get is "Could not fetch meta tags".. I've tried different URLs and such but still wont work ..
Anyone have an idea?
I am trying to pull title and meta description from an URL..
To pull the URL I am using the code from http://www.dreamincode.net/code/snippet4625.htm...
Code: Select all
function getTitle($Url){
$str = file_get_contents($Url);
if(strlen($str)>0){
preg_match("/\<title\>(.*)\<\/title\>/",$str,$title);
return $title[1];
}
}
//Example:
echo getTitle("http://www.cnn.com");Code: Select all
// Assuming the above tags are at www.example.com
$tags = get_meta_tags('http://www.cnn.com/')
or die("Could not fetch meta tags");
// Notice how the keys are all lowercase now, and
// how . was replaced by _ in the key.
echo $tags['author']; // name
echo $tags['keywords']; // php documentation
echo $tags['description']; // a php manual
echo $tags['geo_position']; // 49.33;-86.59Code: Select all
<html>
<head>
</head>
<body>
<?php
function getTitle($Url){
$str = file_get_contents($Url);
if(strlen($str)>0){
preg_match("/\<title\>(.*)\<\/title\>/",$str,$title);
return $title[1];
}
}
//Example:
echo getTitle("http://www.cnn.com");
// Assuming the above tags are at www.example.com
$tags = get_meta_tags('http://www.cnn.com/')
or die("Could not fetch meta tags");
// Notice how the keys are all lowercase now, and
// how . was replaced by _ in the key.
echo $tags['author']; // name
echo $tags['keywords']; // php documentation
echo $tags['description']; // a php manual
echo $tags['geo_position']; // 49.33;-86.59
?>
Test!
</body>
</html>Anyone have an idea?