Using the CURL library in PHP
(Page 3 out of 3)Practical uses
The first useful thing the curl library could be used for is checking whether a page really exists. To do this, we first have to retrieve the page, and then check the response code (404=not found, and thus it doesn't exist). See the example below:
// create a new curl resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.google.com/does/not/exist");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// grab URL
$output = curl_exec($ch);
// Get response code
$response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// Not found?
if ($response_code == '404') {
echo 'Page doesn\'t exist';
} else {
echo $output;
}
?>
Another possibility is to create an automatic link checker, which will get a page, and check if all the links work (by using the above code), and then retrieving each link, and doing the same.
Curl also makes it possible to write your own web spider, similar to Google's web spider, or any other web spider. This article isn't about writing a web spider, so I won't talk about it any further, but a future article on PHPit will show you exactly how to create your own web spider.
Conclusion
In this article I've shown how to use the CURL library, and taken you through most of its options.
For most basic tasks, like simply getting a page, you probably won't need the curl library, since PHP comes with inbuilt support for remote pages. But as soon as you want to do anything slightly advanced, you're probably going to want to use the curl library.
In the near-future I will show you exactly how to build your own web spider, similar to Google's web spider, so stay tuned to PHPit.
If you have any questions or comments on this article, feel free to leave them below, or join us at PHPit Forums.
April 25th, 2006 at 4:15 am
You can also use curl to submit XML requests to XML providers, like credit card clearing houses. While I don’t know how efficent this is below is a small sample.
$xml = ”
$MerchantID
$Account
$OrderID
$Amount
$CardNumber
$CardExpiryDate
$CardType
“;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $PaymentServer);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $xml);
$response = curl_exec ($curl);
curl_close ($curl)
$returnedXML = simplexml_load_string($response);
April 25th, 2006 at 4:17 am
Sorry forgot that you won’t see xml tags, so here I’ll try again;
$xml = ”
$MerchantID
$Account
$OrderID
$Amount
$CardNumber
$CardExpiryDate
$CardType
“;
April 25th, 2006 at 5:59 pm
[…] I’ve heard a few references to CURL but never knew much about it. CURL allows you to scrape/use other web pages as data . The most interesting use (I found) is you can automatically fill out form data and retrieve the $_POST array. CURL… it’s one bad mutha… http://phpit.net/article/using-curl-php/1 […]
April 26th, 2006 at 1:29 pm
Excellent***
Very very easy to understand
April 28th, 2006 at 7:44 am
As a Java/J2EE developer who also happens to use PHP for a few projects, I must conclude that this is the worst HTTP API that I have ever seen. It is not OO; you have to use curl_setopt() to set everything, including the URL and the parameters; by default output is printed to the browser instead of returned to the user, …
Maybe it’s not a bad idea to create a wrapper around it.
April 29th, 2006 at 10:38 am
Jan, there are probably tons of wrappers already on phpclasses.org, and it’d be pretty easy to write your own as well.
August 23rd, 2006 at 1:45 pm
CURL is certainly a good thing. Using this I wrote couple of programs to remotely login to the site and get data posted there. This is simply amazing thing to work on by every PHP programmer.