PHP cURL examples

cURL is a tool that allows applications to send and receive data over the network using URLs. In PHP cURL enables scripts to communicate with external servers, APIs, and services by making HTTP requests such as GET, POST, PUT, and DELETE.

PHP cURL basics

These functions form the core of every cURL request in PHP:

curl_init();      // Initializes a cURL session
curl_setopt();    // Sets options for the cURL session
curl_exec();      // Executes the cURL request
curl_close();     // Closes the cURL session

PHP cURL POST request

A POST request sends data to a server, most often to APIs or form handlers.

<?php

$postRequest = array(
    'firstFieldData' => 'foo',
    'secondFieldData' => 'bar'
);

$ch = curl_init('https://hostname.tld/api');

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postRequest);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);

$data = json_decode($response, true);

PHP cURL GET request

A GET request retrieves data from a server, such as API responses or page content.

<?php

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://hostname.tld/phone-list');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);

$data = json_decode($response, true);

PHP cURL with custom headers

Custom headers are commonly used for APIs, authentication, and content type declarations.

<?php

$ch = curl_init('https://hostname.tld/api');

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Authorization: Bearer YOUR_API_TOKEN',
    'Content-Type: application/json'
));

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);

PHP cURL POST with JSON data

Many modern APIs require JSON instead of form-encoded data.

<?php

$data = array(
    'name' => 'John',
    'email' => 'john@example.com'
);

$jsonData = json_encode($data);

$ch = curl_init('https://hostname.tld/api');

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($jsonData)
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);

$result = json_decode($response, true);

PHP cURL with basic authentication

Some APIs and services require HTTP basic authentication.

<?php

$ch = curl_init('https://hostname.tld/api');

curl_setopt($ch, CURLOPT_USERPWD, 'username:password');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);

PHP cURL file upload

Use this when you need to upload files to an API or remote service.

<?php

$file = new CURLFile('/path/to/file.jpg', 'image/jpeg', 'file.jpg');

$postData = array(
    'file' => $file
);

$ch = curl_init('https://hostname.tld/upload');

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);

PHP cURL error handling

Always check for errors to avoid silent failures.

<?php

$ch = curl_init('https://hostname.tld/api');

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);

if ($response === false) {
    $error = curl_error($ch);
}

curl_close($ch);

PHP cURL get HTTP status code

This helps you debug API responses and server behavior.

<?php

$ch = curl_init('https://hostname.tld/api');

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);

$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

PHP cURL timeout settings

Timeouts prevent scripts from hanging indefinitely.

<?php

$ch = curl_init('https://hostname.tld/api');

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);

$response = curl_exec($ch);
curl_close($ch);

PHP cURL follow redirects

Useful when requesting URLs that return 301 or 302 responses.

<?php

$ch = curl_init('https://hostname.tld');

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

$response = curl_exec($ch);
curl_close($ch);
Hosting with cPanel JetHost

JetHost Experts Tip

Always enable error handling and check the HTTP status code after executing a cURL request. This helps you detect connection issues, authentication problems, and server errors early instead of relying only on the response body.