Using curl in PHP
Install in windows
copy php_curl.dll in c:\php\ext folder. in php.ini activate theb "extension=php_curl.dll" option by removing the ";" in front of the line. Save "php.ini" file. Restart the web server.
Our very basic cUrl example goes like this.
Start the cUrl session with the function curl_init(). To use cURL you have to set the variuos options of cURL using the function 'curl_setopt()'. Here we have used the option 'CURLOPT_URL' to open up the home page of 'koderguru.com'. Also we have set the option 'CURLOPT_HEADER' to include header information of the target page. After setting up the options we execute the function 'curl_exec()' to get the results. Then we close the curl session using 'curl_close()'. After execution of the code you will see the page(Home page of koderguru.com in this case) in your browser
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.koderguru.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
curl_exec($ch);
// close curl resource, and free up system resources
curl_close($ch);
This example opens up the site "http://www.koderguru.com/"
The above example can be extended to find out whether a site/page really exists or not
$creq = curl_init();
curl_setopt($creq, CURLOPT_URL, "http://www.sitenotexist.com");
curl_exec($creq);
//If site was found 'curl_errno($creq)' returns 0 otherwise returns the errorcode of last curl operation
if (curl_errno($creq)) {
print curl_error($creq);
} else {
curl_close($creq);
}
In the above example we are trying to open up the home page of "http://www.sitenotexist.com" (which really does not exist until the creation of this tutorial). As it can not open the page , returns an error code . We can check the error code using ' curl_errno($creq)'. If no error occured it returns '0' otherwise it returns the actual error code.
This code is very usefull if you want to know if a site really exist through code. For example , if you have a link submission page , then you must want to check if the link submitted does really exist before approving it. In this case the above code becomes very useful.