One of several awesome features of PHP is getting remote information from other sources on the Internet such as web page information. You will usually have the need to get data that is located on some other server. You have surely viewed the “link” button in the social networking as well as social bookmarking websites like Google+, Digg etc. Whenever you insert a web link into the field, you will see the website title, Meta description and author details. This functionality is mostly used in social networking websites; however it seems to be well recognized also in several social bookmarking, web directories and RSS aggregators.
If you develop a website which allows submitting links of web pages, it seems helpful to the users that your website automatically gets Meta and title details of their submitting web page. PHP makes pulling these title and Meta information into a string variable an incredibly easy process.
In order to get a remote web page information from PHP, you’ve four different ways to do:
1. By using fopen() function (takes some extra code to get)
2. By using file_get_contents() function (shortest way) (Recommended)
3. By using Curl function (php curl function is disabled in some hosting servers)
4. By making your own custom made function using php’s built-in functions.
So, the best way is to use file_get_contents() function and we will make use of it in order to get the required in information.
$webpage = file_get_contents('http://www.addlinkswebdirectory.com');
The file_get_contents(); function will return the complete source code of the specified web page in the form of a string and it will be saved in $webpage variable.
$source_code = explode("title>", $webpage);
The above line of code is used to divide the source code of the web page into an array of 3 elements.
First element of the array $source_code[0]; will contain the code before title>
Second element $source_code[1]; will be the title of the webpage including Third element $source_code[2]; will contain the rest of the code of the webpage after the title tag. If you echo out this element then it will open the specified web page in your current URL.
$title = str_replace("
echo 'Title: '. $title . ‘
’;