cURL API Requests
cURL क्या है
cURL PHP script को दूसरी websites और APIs को requests भेजने और जवाब पढ़ने देता है। जब आपकी site को payment gateway, SMS service, या weather API से बात करनी हो, cURL आम tool है। हर cURL call चार steps में होती है: init, options set, execute, close।
curl_init() → curl_setopt() → curl_exec() → curl_close()।GET request
किसी URL से data लाइए और string के रूप में वापस पाइए।
<?php
$ch = curl_init("https://api.example.com/data");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
?>
CURLOPT_RETURNTRANSFER मुख्य line है — यह curl_exec को response return करवाता है ताकि आप उसे तुरंत print करने के बजाय $response में store कर सकें।
POST request
Server को data भेजिए, जैसे form submit करने या record बनाने को।
<?php
$ch = curl_init("https://api.example.com/save");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, [
"name" => "Aman",
"class" => 12
]);
$response = curl_exec($ch);
curl_close($ch);
?>
CURLOPT_POST POST पर switch करता है, और CURLOPT_POSTFIELDS में वो data होता है जो आप body में भेजते हैं।
JSON भेजना
ज़्यादातर आधुनिक APIs JSON चाहते हैं। data encode कीजिए और header set कीजिए।
<?php
$data = json_encode(["name" => "Aman"]);
$ch = curl_init("https://api.example.com/save");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Content-Type: application/json"
]);
$response = curl_exec($ch);
curl_close($ch);
?>
Content-Type: application/json header server को बताता है कि body JSON है। जवाब को json_decode($response, true) से decode कीजिए।
Errors जाँचना
<?php
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo "Error: " . curl_error($ch);
}
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo "Status: $code"; // जैसे 200
curl_close($ch);
?>
हमेशा connection errors और HTTP status code जाँचिए (200 यानी OK, 404 not found, 500 server error), ताकि आपका app ठीक से जवाब दे सके।
आम गलतियाँ
CURLOPT_RETURNTRANSFERभूलना। इसके बिना response print होता है, return नहीं, तो$responseखाली रहता है।- JSON header set न करना। कई APIs
Content-Type: application/jsonबिना JSON ठुकरा देते हैं। - Status code अनदेखा करना। Request "चल" सकती है पर 404 या 500 लौटा सकती है — हमेशा जाँचिए।
- Handle बंद न करना। काम पूरा होने पर
curl_close()।
सारांश
- cURL PHP से दूसरी sites और APIs को requests भेजता है।
- चार steps: init, options set, execute, close।
CURLOPT_RETURNTRANSFERresponse को string के रूप में लौटाता है।- POST को
CURLOPT_POST+CURLOPT_POSTFIELDS; JSON को JSON header। - समस्याओं को
curl_errorऔर HTTP status code जाँचिए।
आगे, मिला JSON पढ़िए PHP JSON से, या अपना endpoint बनाइए PHP REST API से।
अक्सर पूछे जाने वाले प्रश्न (FAQ)
PHP में cURL किसलिए इस्तेमाल होता है?
cURL में GET और POST में क्या अंतर है?
curl_setopt($ch, CURLOPT_POST, true)।CURLOPT_RETURNTRANSFER क्यों चाहिए?
CURLOPT_RETURNTRANSFER को true करने से curl_exec response को string के रूप में return करता है, ताकि आप उसे store और process कर सकें।cURL से JSON कैसे भेजें?
CURLOPT_POSTFIELDS से अपने json_encode किए data पर set कीजिए, और CURLOPT_HTTPHEADER से Content-Type: application/json header जोड़िए।cURL request fail हुई या नहीं कैसे जाँचें?
curl_exec के बाद connection problems को curl_error($ch), और server ने error दिया या नहीं देखने को HTTP status code curl_getinfo($ch, CURLINFO_HTTP_CODE) जाँचिए।