ZetCode

PHP curl_init Function

last modified April 11, 2025

The PHP curl_init function initializes a new cURL session. It's the first step in making HTTP requests using PHP's cURL extension. The function returns a cURL handle used for subsequent operations.

Basic Definition

The curl_init function creates a new cURL session handle. It can take an optional URL parameter to initialize the session with a target. The handle must be closed with curl_close() when no longer needed.

Syntax: curl_init(?string $url = null): CurlHandle|false. Returns a cURL handle on success, FALSE on failure. The handle is used with other cURL functions like curl_setopt and curl_exec.

Basic cURL Initialization

This example shows the simplest way to initialize a cURL session.

basic_init.php
<?php

declare(strict_types=1);

$ch = curl_init();

if ($ch === false) {
    die("Failed to initialize cURL");
}

// Set options and perform operations here

curl_close($ch);

We initialize a cURL session without a URL and check for failure. The handle is stored in $ch and must be closed after use. This is the foundation for all cURL operations in PHP.

Initialization with URL

This example demonstrates initializing cURL with a target URL.

init_with_url.php
<?php

declare(strict_types=1);

$url = "https://jsonplaceholder.typicode.com/posts/1";
$ch = curl_init($url);

if ($ch === false) {
    die("Failed to initialize cURL");
}

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

echo $response;

curl_close($ch);

We pass the URL directly to curl_init, which sets CURLOPT_URL automatically. This is a shortcut when you only need to set the URL initially. We still set other options like CURLOPT_RETURNTRANSFER separately.

Multiple cURL Handles

This example shows how to work with multiple cURL handles simultaneously.

multi_handles.php
<?php

declare(strict_types=1);

$ch1 = curl_init("https://jsonplaceholder.typicode.com/posts/1");
$ch2 = curl_init("https://jsonplaceholder.typicode.com/comments/1");

curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);

$response1 = curl_exec($ch1);
$response2 = curl_exec($ch2);

echo "Post: " . $response1 . "\n\n";
echo "Comment: " . $response2;

curl_close($ch1);
curl_close($ch2);

We initialize two separate cURL handles for different endpoints. Each handle maintains its own configuration and can be executed independently. Remember to close all handles when finished.

cURL with Error Handling

This example demonstrates proper error handling with curl_init.

error_handling.php
<?php

declare(strict_types=1);

$ch = curl_init("https://nonexistent.example.com");

if ($ch === false) {
    die("Failed to initialize cURL handle");
}

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);

$response = curl_exec($ch);

if ($response === false) {
    echo "cURL Error: " . curl_error($ch);
} else {
    echo $response;
}

curl_close($ch);

We check if curl_init fails and handle potential errors during execution. The example uses a non-existent domain to demonstrate error scenarios. Proper error handling is crucial for robust applications.

Reusing cURL Handles

This example shows how to reuse a cURL handle for multiple requests.

handle_reuse.php
<?php

declare(strict_types=1);

$ch = curl_init();

if ($ch === false) {
    die("Failed to initialize cURL");
}

// First request
curl_setopt($ch, CURLOPT_URL, "https://jsonplaceholder.typicode.com/posts/1");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response1 = curl_exec($ch);

// Second request
curl_setopt($ch, CURLOPT_URL, "https://jsonplaceholder.typicode.com/posts/2");
$response2 = curl_exec($ch);

echo "First post: " . $response1 . "\n\n";
echo "Second post: " . $response2;

curl_close($ch);

We initialize one handle and reuse it for multiple requests by changing options. This is more efficient than creating new handles for each request. The same options persist unless explicitly changed.

Best Practices

Source

PHP curl_init Documentation

This tutorial covered the PHP curl_init function with practical examples showing its usage for initializing cURL sessions in various scenarios.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all PHP cURL tutorials.