UltraMega Blog
1Dec/091

Using cURL Within PHP

PHP includes an easy to use interface for the cURL library. This means you can easily communicate with other servers using a variety of protocols. It is commonly used to access web service APIs such as Twitter. This tutorial will explain the basics and show some usage examples.

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:

  1. start a cURL session and get a handle (curl_init)
  2. set the options for the session (curl_setopt)
  3. execute the session (curl_exec)
  4. close the session (curl_close).

The handle returned by curl_init is used as the first parameter in the other functions.

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?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);
?>

Filling Out Forms

Let's say a website has a form like this that you want to submit via PHP.

<form method="post" action="form.php">
   <input type="text" name="name" />
   <input type="text" name="color" />
</form>

The example below shows how that can be accomplished. All you need to do is set the appropriate method (CURLOPT_POST), and set an array with the form data (CURLOPT_POSTFIELDS).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?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);
?>

Authentication

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".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?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);
?>

Conclusion

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!

Posted by Steve

Tagged as: , Leave a comment
Comments (1) Trackbacks (0)

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

No trackbacks yet.