Cant get title and meta description from URL?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dominod
Forum Commoner
Posts: 75
Joined: Wed Jun 30, 2010 7:18 am

Cant get title and meta description from URL?

Post by dominod »

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...

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");
And to pull the meta description I am using the code from http://php.net/manual/en/function.get-meta-tags.php.

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.59
So my complete code looks like this:

Code: 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>
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?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Cant get title and meta description from URL?

Post by AbraCadaver »

You didn't say if the get title worked or not, but just a shot in the dark:

Code: Select all

echo ini_get('allow_url_fopen');
And you should be using this while developing:

Code: Select all

error_reporting(E_ALL);
ini_set('display_errors', '1');
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
dominod
Forum Commoner
Posts: 75
Joined: Wed Jun 30, 2010 7:18 am

Re: Cant get title and meta description from URL?

Post by dominod »

I fixed it! 8)

If anyone wanna know, here is the code I used:

Code: Select all

<html>
<head>
</head>
<body>
<?php
$news = $_GET["news"];

function getTitle($Url){
        $str = file_get_contents($Url);
        if(strlen($str)>0){
                preg_match("/\<title\>(.*)\<\/title\>/",$str,$title);
                return $title[1];
        }
}
//Example:
echo getTitle("$news");

echo "<br><br>";


// Assuming the above tags are at www.example.com
$tags = get_meta_tags($news)
 or die("Could not fetch meta tags");


echo $tags['keywords'];  // a php manual
echo "<br><br>";
echo $tags['description'];  // a php manual


?>
</body>
</html>
Post Reply