CodShot
PHP Public

cURL GET Request in PHP

Call an external API with cURL and decode the JSON response.

#php #curl #api #json
PHP
<?php

$ch = curl_init('https://api.example.com/items');

curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_TIMEOUT => 10,
]);

$responseBody = curl_exec($ch);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);

curl_close($ch);

if ($responseBody === false || $statusCode >= 400) {
    throw new RuntimeException('API request failed: ' . $error);
}

$data = json_decode($responseBody, true);

Notes

In production, handle timeouts and log failed external API calls.

Snippet actions

CodShot user
CodShot user
Updated: 2026-06-08 14:33
Public snippets
0
Forks
CodShot AI Help
Ask about CodShot features, plans, workspace, AI limits, referrals, and public help topics.
Hi! I can help you understand how CodShot works. What would you like to know?
For account-specific or sensitive issues, please open a support ticket. Open support