0Pricing
PHP Academy · Lesson

Making HTTP Requests with cURL

Use cURL to perform GET and POST requests to external APIs.

Making HTTP Requests with cURL is a free PHP Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What is cURL?

cURL is a library for transferring data with URLs. PHP wraps it in the curl_* functions, making it the standard way to call external APIs.

Basic GET Request

Initialise a cURL handle, set options, execute, and close.

<?php
$ch = curl_init("https://api.example.com/users");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;

CURLOPT_RETURNTRANSFER

Without this option, curl_exec() prints the response directly. Set it to true to capture the response in a variable.

POST Request

Set CURLOPT_POST and CURLOPT_POSTFIELDS for a POST request.

<?php
$ch = curl_init("https://api.example.com/users");
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST           => true,
    CURLOPT_POSTFIELDS     => json_encode(["name" => "Alice"]),
    CURLOPT_HTTPHEADER     => ["Content-Type: application/json"],
]);
$response = curl_exec($ch);
curl_close($ch);

Setting Headers

Pass an array of header strings to CURLOPT_HTTPHEADER.

<?php
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    "Authorization: Bearer ".$token,
    "Accept: application/json",
]);

Getting the HTTP Status Code

Retrieve the response status code after curl_exec() with curl_getinfo().

<?php
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo $statusCode; // 200, 404, 500, etc.

Error Handling

Check curl_errno() and curl_error() to detect and report connection errors.

<?php
if (curl_errno($ch)) {
    echo "cURL error: ".curl_error($ch);
}
curl_close($ch);

Timeout Options

Set connection and execution timeouts to prevent hanging requests.

<?php
curl_setopt_array($ch, [
    CURLOPT_CONNECTTIMEOUT => 5,  // seconds to connect
    CURLOPT_TIMEOUT        => 15, // max total seconds
]);

Following Redirects

Enable automatic redirect following with CURLOPT_FOLLOWLOCATION.

<?php
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 5);

SSL Verification

By default cURL verifies SSL certificates. Never disable verification in production — only for local development with self-signed certs.

<?php
// Development only — NEVER in production!
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt_array()

Use curl_setopt_array() to set multiple options in one call — cleaner than repeated curl_setopt() calls.

Summary

cURL is PHPs standard HTTP client. Always set CURLOPT_RETURNTRANSFER, check errors with curl_errno, and always close handles with curl_close.

Quick Check

Which option captures cURL response into a variable?

Frequently asked questions

Is the “Making HTTP Requests with cURL” lesson free?

Yes — the full text of “Making HTTP Requests with cURL” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.

What will I learn in “Making HTTP Requests with cURL”?

Use cURL to perform GET and POST requests to external APIs. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start PHP Academy?

No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Making HTTP Requests with cURL” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this PHP Academy lesson?

Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. JSON Encoding and Decoding
  2. Making HTTP Requests with cURL
  3. Handling API Responses and Errors
  4. Building a Simple API Client
← Back to PHP Academy