HTTP Requests with cURL
cURL fetches URLs. Set options with curl_setopt, read the response with CURLOPT_RETURNTRANSFER and decode JSON. Not run here because the example would call the network.
Example
<?php
$ch = curl_init("https://api.example.com/users");
curl_setopt_array($ch, [CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 10]);
$body = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$data = json_decode($body, true);Exercise
Start a request.
("https://x.com")Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.