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 1 out of 3)

Abstract

In this article you will learn what the CURL library is, how to use it, and some of its (advanced) options.

Introduction

Sooner or later you're bound to run across a certain problem in your script: how to retrieve content from other websites. There are several methods for this, and the simplest one is probably to use the fopen() function (if it's enabled), but there aren't really a lot of options you can set when using the fopen function. What if you're building a web spider, and want to have a custom user agent? That isn't really possible with fopen, nor is it possible to define the request method (GET or POST).

That's where the CURL library comes in. This library, usually included with PHP, allows you to retrieve other pages, and also makes it possible to define dozens of different options.

In this article we'll have a look at how to use the CURL library, what it can do, and explore some of its options. But first, let's get started with the basics of CURL.

The Basics

The first step in using CURL is to create a new CURL resource, by calling the curl_init() function, like so:


// create a new curl resource
$ch = curl_init();
?>

Now that you've got a curl resource, it's possible to retrieve a URL, by first setting the URL you want to retrieve using the curl_setopt() function:


// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.google.com/");
?>

After that, to get the page, call the curl_exec() which will execute the retrieval, and automatically print the page:


// grab URL and pass it to the browser
curl_exec($ch);
?>

Finally, it's probably wise to close the curl resource to free up system resources. This can be done with the curl_close() function, as follows:


// close curl resource, and free up system resources
curl_close($ch);
?>

That's all there is to it, and the above code snippets together form the following working demo:

// create a new curl resource
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.google.nl/");

// grab URL and pass it to the browser
curl_exec($ch);

// close curl resource, and free up system resources
curl_close($ch);

?>

(View Live Demo)

The only problem we have now is that the output of the page is immediately printed, but what if we want to use the output in some other way? That's no problem, as there's an option called CURLOPT_RETURNTRANSFER which, when set to TRUE, will make sure the output of the page is returned instead of printed. 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.nl/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// grab URL, and return output
$output = curl_exec($ch);

// close curl resource, and free up system resources
curl_close($ch);

// Replace 'Google' with 'PHPit'
$output = str_replace('Google', 'PHPit', $output);

// Print output
echo $output;

?>

(View Live Demo)

In the previous two examples you might've noticed we used the curl_setopt() function to define how the page should be retrieved, and that's where the real power of curl lies. By setting all kinds of different options, pretty much anything is possible, so let's have a look at that a bit more.

Next: 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