A quick and simple tutorial on how to use cURL with PHP

Post to Twitter

The Client URL Library is a very powerful and commonly used library. With it your can upload files, get your Twitter status, scrape websites, etc. Today I’m going to use PHP’s cURL library to call a simple local service.

To do this demo you will need to have PHP installed along with having cURL enabled. I have four articles on how to get PHP installed on Ubuntu in case you need help.
1. Install Lighttpd, PHP and MySQL on Ubuntu 10.10
2. MySQL, PHP 5) and phpMyAdmin on Ubuntu 10.10
3. Building a LAMP (Apache, PHP, MySQL) server on Ubuntu 10.10 with Tasksel
4. Install PHP 5, Apache 2 and MongoDB on Ubuntu 10.10 and test it out

You will need to create two PHP files and place them into a folder that your web server can serve PHP files from. In my case I put them into /var/www/ for simplicity. The two files were named: using-curl.php and helloservice.php.

helloservice.php is very simple and essentially just echoes back any data it gets:

<?php
  echo 'Your message was: ' . $_REQUEST["message"] . ' and your name is: ' . $_REQUEST["name"];
?>

using-curl.php has the real magic:

<?php
  // Here is the data we will be sending to the service
  $some_data = array(
    'message' => 'Hello World', 
    'name' => 'Chad'
  );  

  $curl = curl_init();
  // You can also set the URL you want to communicate with by doing this:
  // $curl = curl_init('http://localhost/echoservice');
  
  // We POST the data
  curl_setopt($curl, CURLOPT_POST, 1);
  // Set the url path we want to call
  curl_setopt($curl, CURLOPT_URL, 'http://localhost/helloservice.php');  
  // Make it so the data coming back is put into a string
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  // Insert the data
  curl_setopt($curl, CURLOPT_POSTFIELDS, $some_data);
  
  // You can also bunch the above commands into an array if you choose using: curl_setopt_array
  
  // Send the request
  $result = curl_exec($curl);
  // Free up the resources $curl is using
  curl_close($curl);
  
  echo $result;
?>

curl_init initializes the cURL session and you can pass in a URL if you choose to but I think its more flexible to use CURLOPT_URL instead. Pick whatever works best for you.

curl_setopt is where the options are set for the cURL session, in my case I used: CURLOPT_POST, CURLOPT_URL, CURLOPT_RETURNTRANSFER, and CURLOPT_POSTFIELDS.

Finally I set everything in motion by calling curl_exec and then cleaning up the resources by calling curl_close.

The results are just as we expect them to be:

Read over all the options for curl_setopt. Keep in mind you can do things like set a timeout (CURLOPT_TIMEOUT, CURLOPT_TIMEOUT_MS) as well as get error feedback using curl_error.

curl_getinfo will supply you with information regarding the transfer. So if you wanted to get some information back you could do something like this:

<?php
  // Here is the data we will be sending to the service
  $some_data = array(
    'message' => 'Hello World', 
    'name' => 'Chad'
  );  

  $curl = curl_init();
  // You can also set the URL you want to communicate with by doing this:
  // $curl = curl_init('http://localhost/echoservice');
  
  // We POST the data
  curl_setopt($curl, CURLOPT_POST, 1);
  // Set the url path we want to call
  curl_setopt($curl, CURLOPT_URL, 'http://localhost/helloservice.php');  
  // Make it so the data coming back is put into a string
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  // Insert the data
  curl_setopt($curl, CURLOPT_POSTFIELDS, $some_data);
  
  // You can also bunch the above commands into an array if you choose using: curl_setopt_array
  
  // Send the request
  $result = curl_exec($curl);
  
  // Get some cURL session information back
  $info = curl_getinfo($curl);  
  echo 'content type: ' . $info['content_type'] . '<br />';
  echo 'http code: ' . $info['http_code'] . '<br />';
  
  // Free up the resources $curl is using
  curl_close($curl);
  
  echo $result;
?>

Result:

Post to Twitter

This entry was posted in PHP. Bookmark the permalink.

4 Responses to A quick and simple tutorial on how to use cURL with PHP

  1. Pingback: A quick and simple tutorial on how to use cURL with PHP

  2. Barry says:

    Hello can you give me an example of a html form to use so that the following hardcoded fields can be a variable eg from a users form input?

    ‘message’ => ‘Hello World’,
    ‘name’ => ‘Chad’

  3. Thanks alot! very helpful :)

  4. al says:

    Thanks for sharing, it would be nice if you add more example. Thx

Comments are closed.