Sunday, February 1, 2009

Curly use of cURL in php

I's interested in cURL in php since when Rashim was looking for something to get the yahoo weather feed as it is. He made it using cURL and I just loved it- amazed with versatile use of cURL.

I's looking for a way to get the all the statistics and searching facilities for expired or deleted domain names. And finally I decided to use cURL to search from some free service providers on the net although the usual approach I got is to store the names performing some scheduled search from who-is servers.
Anyway, here I'm gonna share all I've been playing with cURL :

Getting required html content from a website using cURL :
$url = "http://www.example.com/domains/detailedStatistics";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$curl_scraped_page = curl_exec($ch);
curl_close($ch);
$regex = '~(.*?)
~s';
preg_match($regex,$curl_scraped_page,$match);
//var_dump($match);
echo "Detailed domain statistics:
";
echo "
". $match[0]."
";

?>

Searching on a website using cURL and parsing the html to get desired result:

I used a html form to post input values to a searcher file. The code on the searcher file looked like :
//A Beautiful html dom parser
include('./simple_html_dom.php');
$cat = explode("?",$_SERVER['REQUEST_URI']);

$url = "http://www.example.com/Domain_Name_Search_Results.aspx?".$cat[1];

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$curl_scraped_page = curl_exec($ch);
curl_close($ch);

// find all tabele tags with id=results
$html = str_get_html($curl_scraped_page);


$thetable = $html->find('table[class=BlackLeftRight]',0);
foreach($thetable->find('img') as $img)
{
$img->outertext = "";
}
echo "".$thetable->innertext."
" ;

?>
That's it :) Happy curling then :)

Reference: