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.
Code: Set the URL of the Web Page
$url = “http://www.mistonline.in”;
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.
Code: 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.
Code: 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.
Code: 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):
Code: 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.
Code: Complete Code
$url = ”http://www.tutorialz.tk/”;
$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”;
?>
Incoming search terms:
- how to get list of webpages from url in php (9)
- get remote web page info with php (6)
- read title tag php from webpage (4)
- PHP display remote website (4)
- php display remote web page (3)
- php load remote web page (3)
- php get remote web page (3)
- php get remote page title (3)
- php retrieve remote webpage (2)
- php get remote content (2)
- read the contents of a remote web page javascript (2)
- php get remote webpage (2)
- read data using javascript to remote site (2)
- php get content from remote website (2)
- php display remote form on webpage (2)
- php show remote page (2)
- php remote page (2)
- php get remote page (2)
- php grab title from remote webpage (2)
- using variable from other php webpages in title tag (2)
- php get remote page title and description (2)
- how to open remote site content in myapplication using php (2)
- wordpress parse remote site data (2)
- php reading data from remote sites (1)
- php read remote website code (1)
- php reading web site contents (1)
- php print all tags of remote page (1)
- php load div remote (1)
- php remote content display (1)
- php parse remote page title (1)
- php remote keywords (1)
- php remote web page into variable (1)
- php remote webpage (1)
- php remote webpage load time (1)
- php get request vars from remote site (1)
- php GET url title description script tutorial (1)
- php load remote html page (1)
- php insert remote web page (1)
- tutorials on how to parse webpages with php (1)
- php load var with remote web page (1)
You will also be interested in ,
- Randomly read and display values of an xml file using php
- How to display popular posts in wordpress
- Zip or Archive a directory using php
- Speed up wordpress using .htaccess part 1
- How to create random passwords using php
- Php mysql example image gallery blob storage
- Simple Example Of Database Connection Using PEAR In PHP
- PHP E-mail with attachment
- Display text file line by line and delete certain lines using php
- Total Number Of Rows In MYSQL
