Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

curl_init

Creates a cURL request.

Syntax

curl_init(?string $url = null): CurlHandle|false

Parameters

ParameterTypeDescription
$url?stringOptional. The request address.

Return value

CurlHandle|falseThe request object, or `false`.

Examples

A GET request:

<?php
 
// rates.php
 
$curl = curl_init('https://api.example.com/rates');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 5);
 
$body = curl_exec($curl);
 
echo curl_getinfo($curl, CURLINFO_HTTP_CODE);

Notes

The request is sent only when curl_exec() is called. The object needs no closing — it is freed automatically.

See also

Related recipes

In the tutorial

  • Calling other sites — How to call someone else's API from PHP — file_get_contents, the cURL extension, sending JSON, headers, the response code and error handling.

Thanks for reading!

Donate to the site — we work hard for you.