php Tutorials
Creating An RSS Feed With PHP And MySQL
Retrieving XML With Curl and SimpleXML
Using curl to Query Remote Servers
MySQL Basics
PHP mySQL Voting Booth
File Download Security
PHP Fundamentals

Using curl to Query Remote Servers

Curl Introduction

curl is the client URL function library. PHP supports it through libcurl. To enable support for libcurl when installing PHP add --with-curl=[location of curl libraries] to the configure statement before compiling. The curl package must be installed prior to installing PHP. Most major functions desired when connecting to remote web servers are included in curl, including POST and GET form posting, SSL support, HTTP authentication, session and cookie handling.

To start a curl session use the curl_init() function. Options for the curl session are set via the curl_setopt() PHP function. Once you have the options set execute the request with the curl_exec() function.

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, '');
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec();

In the previous example we first set the handle of the curl session to $ch. Then with the curl_setopt command we set the URL of the request to The CURLOPT_HEADER option sets whether or not the server response header should be returned. By default curl will display the response straight to the browser as the script is executed. To counter this we enabled the CURLOPT_RETURNTRANSFER option. Now when we run the curl_exec() statement the data returned from the remote server is returned and stored the the $data variable instead of passed to the browser.

 1 2 3 4 5  >> Curl and form data
New Content