Hi guys ..

Here I am going to share how to fetch/extract url datas like title , description and images from a website like facebook attach url module

I have used PHP curl to fetch the webpage content as a string. And I have used PHP Domdocument object to extract the meta title and description part.

Finally I have used preg_match_all function to fetch images from the webpage

here is the code below

<pre>function file_get_contents_curl($url)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    $data = curl_exec($ch);
	$info = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
	
	//checking mime types
	if(strstr($info,'text/html')) {
		curl_close($ch);
    	return $data;
	} else {
		return false;
	}
}

//fetching url data via curl
$html = file_get_contents_curl($url);



//parsing title and description begins here:
$doc = new DOMDocument();
@$doc->loadHTML($html);
$nodes = $doc->getElementsByTagName('title');

//get and display what you need:
$title = $nodes->item(0)->nodeValue;
$metas = $doc->getElementsByTagName('meta');

for ($i = 0; $i < $metas->length; $i++)
{
    $meta = $metas->item($i);
    if($meta->getAttribute('name') == 'description')
        $description = $meta->getAttribute('content');
}

//fetch images
$image_regex = '/<img&#91;^>]*'.'src=[\"|\'](.*)[\"|\']/Ui';
preg_match_all($image_regex, $html, $img, PREG_PATTERN_ORDER);
</pre>

Check out my latest facebook scripts

  1. Facebook Style Youtube Video Expander with JQuery – http://w3lessons.info/2013/05/08/facebook-style-youtube-video-expanding-with-jquery/
  2. Facebook Style Wall Script 3.0 – http://w3lessons.info/2013/04/21/facebook-wall-script-3-0-timeline-oauth-location-sharing-smileys-many-more/

View Demo Download

If you enjoyed this article, Get email updates (It’s Free)
http://w3lessons.info/wp-content/uploads/2012/01/facebook-like-fetch-url-data1.pnghttp://w3lessons.info/wp-content/uploads/2012/01/facebook-like-fetch-url-data1-150x150.pngKarthikeyan KfacebookjqueryPHPcurl,facebook,php,url data extract
Hi guys .. Here I am going to share how to fetch/extract url datas like title , description and images from a website like facebook attach url module I have used PHP curl to fetch the webpage content as a string. And I have used PHP Domdocument object to extract the...