By using this site you agree to the use of cookies.

Learn more

Share via:

PHP: HTTP Requests (cURL)

How to use cURL to send HTTP GET and POST requests from PHP applications.

Edited: 2017-05-07 00:15

PHP article image

cURL can be used to send HTTP requests from PHP scripts, in some circumstances this may be more desirable than using PHP's build in functions. For example, sometimes cURL is available when other functions has been disabled by the host.

There does not seem to be any obvious advantage of using cURL over PHP's build in functions, so it is probably mostly about personal preference which method to use.

GET requests

Sending a GET request with cURL is done using a combination of curl_init, curl_setopt, curl_exec, and curl_close.

curl_init initializes a cURL session, in this script, referenced in via the $ch variable. The curl_setopt function can be used to set different options for our session, while curl_exec is used to send the request.

  // URL to fetch
  $url = "https://beamtic.com/";

  // Initialize cURL session
  $ch = curl_init($url);

   // Option to Return the Result, rather than just true/false
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

  // Perform the request, and save content to $result
  $result = curl_exec($ch);

  // Close the cURL resource, and free up system resources!
  curl_close($ch);

  // Shows the result!
  echo $result;

The curl_close function is used to close the session. Generally, PHP will clean up when a script finishes running. But, it is recommended to include it anyway, presumably to avoid potential mistakes by not having it.

POST requests

When sending a POST request with cURL, you should beware that providing the post fields in an array is likely to cause problems in some cases, as it will cause cURL to send the request as multipart/form-data with a boundary, instead of sending it as application/x-www-form-urlencoded – this is rather unexpected behavior.

We need to do two things to send a post request, set the CURLOPT_POST option to true, and include the post fields via the CURLOPT_POSTFIELDS option. Both can be set using the curl_setopt function, before executing the request.

A simple POST request is performed as follows:

$url = "http://beamtic.com/Examples/http-post.php"; // The POST URL
// The POST Data
$postdata  = "name=Jacob";
$postdata .= "&bench=150";

$ch = curl_init($url);

// Set the request type to POST
curl_setopt($ch, CURLOPT_POST, true);
// Pass the post parameters as a naked string
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);

// Option to Return the Result, rather than just true/false
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Perform the request, and save content to $result
$result = curl_exec($ch);

// Show the result?
echo $result;

The important part in the above script is the part where you set the request-type to POST, and pass the data to be posted:

// Set the request type to POST
curl_setopt($ch, CURLOPT_POST, true);
// Pass the post parameters as a naked string
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);

The variable $postdata contains a string where each parameter is separated by unencoded ampersands. To easily add and remove parameters in a very read-able way, you can do like this:

// The POST Data
$postdata  = "name=Jacob";
$postdata .= "&bench=150";

This is just as clean and readable as using an array.

Sometimes the data in some post fields contains special characters, these will need to be encoded with PHP's urlencode function. The server handling the request should automatically decode them, without the need for extra coding.

$postdata = "name=" . urlencode($userinput);
$postdata .= "&bench=150";