Tuesday, 15 January 2013

Get data or Content from a URL using cURL PHP | PHP Tutorials for beginners


There are situation when you need to fetch data from certain URL. This URL can give you RSS feeds or it can be a JSON data.This tutorial of php code for beginners show you how to read content or data from URL using cURL in php.

<?php
$url = www.phpcodeforbeginner.blogspot.in;
if(isset($url)){

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);

$data = curl_exec($ch);
curl_close($ch);

return $data;
}

$html = file_get_contents_curl($url);
//$html contain the whol data from the url specified by you.
}
?>
 Now if you want to just read the title of perticular url for your seo perpose then add the below code after $html.

$doc = new DOMDocument();
@$doc->loadHTML($html);
$nodes = $doc->getElementsByTagName('title');

//get and display what you need:
$title = $nodes->item(0)->nodeValue;
//Title of url get stored in $title.

Hope this php tutorial is useful for you. Keep following www.phpcodeforbeginner.blogspot.in for more help.

2 comments:

  1. dear sir

    very -very thanks for hepling this code , this code is working correctly.....

    ReplyDelete
  2. cccam server23 July 2013 23:48

    dear i need a tutorial how to get specific page content of html page with php curl function.

    ReplyDelete