Lesson 18 โข Advanced
Working with REST APIs ๐
Master cURL options, PHP streams, and Guzzle HTTP client โ three ways to consume external APIs in production PHP applications.
What You'll Learn in This Lesson
- โข Advanced cURL: timeouts, SSL, redirects, and error handling
- โข PHP streams: make HTTP requests without extensions
- โข Guzzle HTTP: professional API client with promises and middleware
- โข Compare cURL vs Streams vs Guzzle for different use cases
- โข Build reusable HTTP client wrappers
cURL Deep Dive
cURL is PHP's most powerful HTTP tool. It supports every HTTP method, custom headers, authentication, file uploads, SSL verification, and connection pooling. The downside is verbosity โ even a simple GET request needs 6+ lines. But understanding cURL gives you full control over every aspect of the HTTP request.
Try It: Advanced cURL
Build robust HTTP requests with error handling, timeouts, and SSL
// Deep Dive: cURL in PHP
console.log("=== cURL: The Swiss Army Knife of HTTP ===");
console.log();
console.log("1๏ธโฃ GET REQUEST with Error Handling");
console.log("โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ");
console.log();
console.log(" function httpGet($url, $headers = []) {");
console.log(" $ch = curl_init($url);");
console.log(" curl_setopt_array($ch, [");
console.log(" CURLOPT_RETURNTRANSFER => true,");
console.log(" CURLOPT_TIMEOUT => 30,");
console.log("
...Guzzle & PHP Streams
For production applications, Guzzle is the industry standard. It provides an elegant, object-oriented API with built-in support for async requests, middleware, and automatic JSON decoding. PHP Streams are a lightweight alternative when you can't install Composer packages โ they work out of the box on every PHP installation.
Try It: Guzzle & Streams
Compare three HTTP approaches and build a Guzzle-style client
// Guzzle HTTP Client & PHP Streams
console.log("=== PHP Streams (Built-in, No Extensions) ===");
console.log();
console.log("file_get_contents() can make HTTP requests!");
console.log();
console.log(" $context = stream_context_create([");
console.log(" 'http' => [");
console.log(" 'method' => 'POST',");
console.log(" 'header' => 'Content-Type: application/json',");
console.log(" 'content' => json_encode(['name' => 'Alice']),");
console.log(" 'timeout' =
...โ ๏ธ Common Mistakes
CURLOPT_SSL_VERIFYPEER = false in production. This makes your app vulnerable to man-in-the-middle attacks.CURLOPT_TIMEOUT, a slow API can hang your entire PHP process. Always set a reasonable timeout (10-30 seconds).curl_close($ch) leaks memory. Guzzle handles this automatically.๐ Quick Reference โ HTTP Tools
| Tool | Best For | Install |
|---|---|---|
| cURL | Full control, complex requests | Built-in ext |
| PHP Streams | Quick scripts, no dependencies | Always available |
| Guzzle | Production apps, async, middleware | composer require guzzlehttp/guzzle |
| Symfony HTTP | Symfony ecosystem | composer require symfony/http-client |
๐ Lesson Complete!
You can now consume any REST API from PHP! Next, build your own micro MVC framework from scratch.
Sign up for free to track which lessons you've completed and get learning reminders.