🔗 PHP cURL Basics – Send HTTP Requests with PHP
cURL is a powerful tool in PHP used for making HTTP requests. You can use it to fetch data from APIs, send POST requests, and even interact with websites programmatically. 🚀
PHP provides the cURL
library for handling HTTP requests effortlessly.
🛠️ Enabling cURL in PHP
Before using cURL, ensure it’s enabled in your PHP installation.
- For Windows: Uncomment
extension=curl
in php.ini - For Linux/Mac: cURL is usually enabled by default
- To check if cURL is available, run:
phpinfo();
🌍 Fetching Data with GET Request
The simplest way to use cURL is by fetching data from a URL.
✅ Example: Basic cURL GET Request
Let’s fetch data from an API.
<?php $url = "https://jsonplaceholder.typicode.com/posts/1"; $ch = curl_init($url); // Initialize cURL curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Get response as a string $response = curl_exec($ch); // Execute request curl_close($ch); // Close cURL echo $response; // Display fetched data ?>
🔍 Output: JSON response from the API.
📨 Sending Data with POST Request
You can send data using a POST request with cURL.
✅ Example: Sending Data via POST
Let’s send a new post request to an API.
<?php $url = "https://jsonplaceholder.typicode.com/posts"; $data = array( "title" => "Hello World", "body" => "This is a test post.", "userId" => 1 ); $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json")); $response = curl_exec($ch); curl_close($ch); echo $response; ?>
🔍 Output: JSON response confirming the post was created.
🔄 Sending Headers with cURL
Sometimes, APIs require headers (e.g., authentication tokens). Here’s how to add them.
✅ Example: Sending Headers in a cURL Request
<?php $url = "https://jsonplaceholder.typicode.com/posts/1"; $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array( "Authorization: Bearer YOUR_ACCESS_TOKEN", "Content-Type: application/json" )); $response = curl_exec($ch); curl_close($ch); echo $response; ?>
🔍 Output: API response with authentication.
📝 Handling cURL Errors
To handle errors, use curl_error()
.
✅ Example: Detecting cURL Errors
<?php $url = "https://invalid-url.com"; $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); if ($response === false) { echo "❌ cURL Error: " . curl_error($ch); } else { echo $response; } curl_close($ch); ?>
🔍 Output: Displays an error message if the request fails.
🎯 Summary
- ✅ Use
curl_init()
to start a cURL session. - ✅ Use
curl_setopt()
to configure requests. - ✅ Use
curl_exec()
to execute the request. - ✅ Use
curl_close()
to free resources. - ✅ Always handle errors with
curl_error()
.
🚀 Next Steps
Now that you know PHP cURL basics, try making requests to real APIs like weather services, payment gateways, or social media APIs! 🌍