Making HTTP Requests with cURL
Use cURL to perform GET and POST requests to external APIs.
What is cURL?
cURL is a library for transferring data with URLs. PHP wraps it in the curl_* functions, making it the standard way to call external APIs.
Basic GET Request
Initialise a cURL handle, set options, execute, and close.
<?php
$ch = curl_init("https://api.example.com/users");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;All lessons in this course
- JSON Encoding and Decoding
- Making HTTP Requests with cURL
- Handling API Responses and Errors
- Building a Simple API Client