If you run any website that accepts submitted links, be it a tutorial website, a directory or a bookmarking site, you may find it useful to automatically retrieve information about a URL. Wouldn’t it be nice if you ran a web directory and you could bring up the title, keywords and description of a URL without the user having to enter it? This tutorial shows you how this can be achieved via PHP.
Firstly we need to set the URL of the web page. Note that it must contain the http prefix as we are going to load some data in through a HTTP file stream.
Set the URL of the Web Page
$url = “http://www.drquincy.com/”;
Now that’s done we need to get the contents of the title tag. We do this by open up a file stream using file() and reading the contents of the page in and storing it in a variable.
Load in the File
$fp = fopen( $url, ‘r’ );
$content = “”;
while( !feof( $fp ) ) {
$buffer = trim( fgets( $fp, 4096 ) );
$content .= $buffer;
}
Then using some we can get the contents of the title tag.
Get Contents of the Title Tag
$start = ‘<title>’;
$end = ‘<\/title>’;
preg_match( “/$start(.*)$end/s”, $content, $match );
$title = $match[ 1 ];
The keywords and description of a web page are stored in the page’s meta tags. Fortunately, PHP has built-in functionality to get meta tag contents so it will be easier to do than the title tag. We call the get_meta_tags() function, which copies all meta tags into an associative array. We then copy the elements of the array we need to some variables.
Get Meta Tags
$metatagarray = get_meta_tags( $url );
$keywords = $metatagarray[ "keywords" ];
$description = $metatagarray[ "description" ];
Then all that remains is to output the details (use the variables how you like in your web project):
Output Details
echo “<div><strong>URL:</strong> $url</div>\n”;
echo “<div><strong>Title:</strong> $title</div>\n”;
echo “<div><strong>Description:</strong> $description</div>\n”;
echo “<div><strong>Keywords:</strong> $keywords</div>\n”;
Here’s the complete code listing.
Complete Code
<?php
$url = “http://www.drquincy.com/”;
$fp = fopen( $url, ‘r’ );
$content = “”;
while( !feof( $fp ) ) {
$buffer = trim( fgets( $fp, 4096 ) );
$content .= $buffer;
}
$start = ‘<title>’;
$end = ‘<\/title>’;
preg_match( “/$start(.*)$end/s”, $content, $match );
$title = $match[ 1 ];
$metatagarray = get_meta_tags( $url );
$keywords = $metatagarray[ "keywords" ];
$description = $metatagarray[ "description" ];
echo “<div><strong>URL:</strong> $url</div>\n”;
echo “<div><strong>Title:</strong> $title</div>\n”;
echo “<div><strong>Description:</strong> $description</div>\n”;
echo “<div><strong>Keywords:</strong> $keywords</div>\n”;
?>


This site is interesting as well as informative. Enjoyed browsing through the site. Keep up the good work. Greetings..
Great article. Recently I had a need similar to waht this article described – to read the content of web pages in a program.
But my question is a bit even harder:
how to get info of remote web pages that can only read after logging in?
Tim, can you help me? PHP or other language will be ok. Thanks in advance.
Harry
Hi Harry,
Yes, that can be done although it becomes more complex than the above. I think (although I am not 100% sure) you can use fsockopen to send POST data (the login credentials) via port 80 – or a different port if using SSL – to the server and login that way.
Hi,
I have added this script in my phpmyadmin,But it showed an error.It said that-
“#1064 – You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘<?php".
Also,I want the content of the webpage intead of the title..Please rely for this..
Thank you!
Not quite sure, what do you mean you’ve ‘added’ it to phpMyAdmin?
For the whole page use file_get_contents().
Hi,
Yes,I tried to install the script in phpmyadimin,But it showed an error.I am an beginner to phpmyadmin and I am beginning right from the zero…I would like to install a script in my phpmyadmin database where- i can copy the contents of a website and sent it to my members mobile phones by SMS…Actualy my intention is to send the college attendance and updates through SMS to my members…Is it possible to make such a program in php???Please help me…