ASPit - Totally ASP JSit - Totally JavaScript
Search PHPit

Use this textbox to search for articles on PHPit. Seperate keywords with a space.

Advertisements

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

?>

(View Live Demo)

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.

« Previous: Options & Info



7 Responses to “Using the CURL library in PHP”

  1. AJ Says:

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

  2. AJ Says:

    Sorry forgot that you won’t see xml tags, so here I’ll try again;
    $xml = ”
    $MerchantID
    $Account
    $OrderID
    $Amount

    $CardNumber
    $CardExpiryDate
    $CardType

    “;

  3. Andy Boyd » Using Curl Says:

    […] 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  […]

  4. sandip wankhede Says:

    Excellent***
    Very very easy to understand

  5. Jan Says:

    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.

  6. Dennis Pallett Says:

    Jan, there are probably tons of wrappers already on phpclasses.org, and it’d be pretty easy to write your own as well.

  7. Arvind Jagtap Says:

    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.

Leave a Reply

About the author
Dennis Pallett is the main contributor to PHPit. He owns several websites, including ASPit and Chill2Music. He is currently still studying.
Article Index
  1. The Basics
  2. Options & Info
  3. Practical Uses & Conclusion
Bookmark Article
Download Article
PDF
Download this article as a PDF file