Jump to content

Cant get title and meta description from URL?


dominod

Recommended Posts

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

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.

// 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:

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

 

 

 

Link to comment
Share on other sites

I fixed it!  8)

 

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

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.