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:
- get remote web page info with php (7)
- php display remote web page (3)
- php get remote page title (3)
- php load remote web page (3)
- php get remote web page (3)
- how to write code using table2csv() (2)
- read data using javascript to remote site (2)
- php get content from remote website (2)
- read the contents of a remote web page javascript (2)
- php retrieve remote webpage (2)
- php remote page (2)
- how to open remote site content in myapplication using php (2)
- php show remote page (2)
- php display remote form on webpage (2)
- php grab title from remote webpage (2)
- php get remote webpage (2)
- php get remote content (2)
- using variable from other php webpages in title tag (2)
- php get remote page title and description (2)
- php get remote page (2)
- php include remote web page (1)
- php read directory contents get page title (1)
- php read in remote page content and search it (1)
- php read remote website code (1)
- php reading data from remote sites (1)
- php reading web site contents (1)
- php load remote html page (1)
- php remote content display (1)
- php remote content in div (1)
- php remote keywords (1)
- php print all tags of remote page (1)
- php parse remote page title (1)
- php grab content of a div from a remote page (1)
- php load div remote (1)
- taking info from a webpage using php mist (1)
- php load remote page (1)
- php get web page contents fget (1)
- PHP get remote web page info (1)
- perform a php search on remote site (1)
- php GET url title description script tutorial (1)
You will also be interested in ,
- How to create random passwords using php
- Find Absolute Path Of A File Or Directory Using PHP
- Extract domain name from url using php
- Reading file using php
- Export MySQL to CSV (Excel) using php
- Convert Month Name To Month Number Using Simple php
- Simple PHP Login Script
- Post Data To Another Website Using cURL In PHP
- Find out which mode php is running CGI or DSO mode
- Replacing a string using php

