curl_init
Creates a cURL request.
Syntax
curl_init(?string $url = null): CurlHandle|falseParameters
| Parameter | Type | Description |
|---|---|---|
| $url | ?string | Optional. 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
- curl_setopt — Sets an option of a cURL request.
- curl_exec — Executes a cURL request.
- file_get_contents — Reads a whole file into a string.
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.