🔴 Advanced · Lesson 32
cURL API Requests
cURL API Requests
What is cURL?
cURL lets PHP send HTTP requests to other servers/APIs — like fetching data or posting to a remote service.
GET Request
$ch = curl_init("https://api.example.com/users");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response, true);
POST Request
$ch = curl_init("https://api.example.com/login");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(["user"=>"aman"]));
$response = curl_exec($ch);
curl_close($ch);
Summary
- cURL sends HTTP requests from PHP to external APIs.
- CURLOPT_RETURNTRANSFER returns the response; CURLOPT_POST for POST.
cURL क्या है?
cURL PHP को दूसरे servers/APIs को HTTP requests भेजने देता है — जैसे data लाना या remote service पर post करना।
GET Request
$ch = curl_init("https://api.example.com/users");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response, true);
POST Request
$ch = curl_init("https://api.example.com/login");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(["user"=>"aman"]));
$response = curl_exec($ch);
curl_close($ch);
सारांश
- cURL PHP से external APIs को HTTP requests भेजता है।
- CURLOPT_RETURNTRANSFER response लौटाता है; POST के लिए CURLOPT_POST।