UltraMega Blog

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.

Tagged as: , Continue reading