There are 4 main functions you need to know to use cURL: curl_init, curl_setopt, curl_exec, and curl_close. The process generally goes like this:
- start a cURL session and get a handle (curl_init)
- set the options for the session (curl_setopt)
- execute the session (curl_exec)
- close the session (curl_close)
All the available options are listed on the curl_setopt manual page with detailed descriptions.
Simple Example
This example shows how to simply grab the contents of a web page. The data is stored in a variable so you can do whatever you want with it. This can be applied to any file in any format (such as XML). The result is identical to using file_get_contents, but faster.
Code: Select all
<?php
// specify the URL to request
$url = 'http://www.example.com/index.htm';
// create cURL session
$ch = curl_init();
// set the URL
curl_setopt($ch, CURLOPT_URL, $url);
// return the response instead of printing
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// send the request and store the response in $resp
$resp= curl_exec($ch);
// end the session
curl_close($ch);
?>
Let's say a website has a form like this that you want to submit via PHP.
Code: Select all
<form method="post" action="form.php">
<input type="text" name="name" />
<input type="text" name="color" />
</form>
Code: Select all
<?php
// specify the URL to request
$url = 'http://www.example.com/form.php';
// set up data to send to the form
$data = array('name' => 'Joe', 'color' => 'red');
// create cURL session
$ch = curl_init();
// set the URL
curl_setopt($ch, CURLOPT_URL, $url);
// return the response instead of printing
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// set request method to POST
curl_setopt($ch, CURLOPT_POST, true);
// set the data to send
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
// send the request and store the response in $resp
$resp= curl_exec($ch);
// end the session
curl_close($ch);
?>
One advantage to using cURL is that is allows you to easily handle authentication. This is useful since some APIs use HTTP authentication. You just need to set the authentication type (CURLOPT_HTTPAUTH) and the credentials (CURLOPT_USERPWD). Credentials are a string in the format "username:password".
Code: Select all
<?php
// specify the URL to request
$url = 'http://www.example.com/private/index.htm';
// specify your credentials
$auth = 'username:password';
// create cURL session
$ch = curl_init();
// set the URL
curl_setopt($ch, CURLOPT_URL, $url);
// return the response instead of printing
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// set the type of authentication
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
// set the credentials
curl_setopt($ch, CURLOPT_USERPWD, $auth);
// send the request and store the response in $resp
$resp= curl_exec($ch);
// end the session
curl_close($ch);
?>
As you can see, cURL is really simple to use once you understand the basic process. You basically tell cURL what you want by setting options, and it does the rest. Any comments or suggestions are welcome, and if you have more examples post away!