← Back to PHP Course | Chapter 14: Advanced PHP | Lesson 5 of 24

PHP cURL Introduction

cURL PHP को आपके लिए दूसरी websites visit करने देता है, किसी दूसरी shop से कुछ लाने के लिए एक helper भेजने जैसा। यह data माँग सकता है या कुछ भेज सकता है और reply वापस ला सकता है।
Syntax
php
$ch = curl_init("https://example.com/path");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);   // return response as a string
$response = curl_exec($ch);
curl_close($ch);

cURL क्या है?

cURL PHP को एक HTTP client की तरह act करने की ability देता है, server-side scripts को दूसरी web services से data fetch या भेजने देते हुए -- किसी external API से बात करने वाली किसी भी चीज़ के लिए ज़रूरी।

उदाहरण: What is cURL?

php
<?php
// Print "cURL lets PHP act as an HTTP client to talk to other web services." to the output
echo "cURL lets PHP act as an HTTP client to talk to other web services.";
// Print `"\n" . (function_exists('curl_init') ? "cURL extension is available" : "cURL not available")` to the output
echo "\n" . (function_exists('curl_init') ? "cURL extension is available" : "cURL not available");
?>

Basic GET Request

एक basic request एक fixed pattern follow करता है: curl_init() एक session खोलता है, curl_setopt() target URL और options configure करता है, curl_exec() request भेजता है, और curl_close() बाद में resources release करता है।

उदाहरण: Basic GET Request

php
<?php
// Declare `$ch`, set to `curl_init("https://example.com")`
$ch = curl_init("https://example.com");
// Call `curl_setopt($ch, CURLOPT_RETURNTRANSFER, true)`
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Declare `$response`, set to `curl_exec($ch)`
$response = curl_exec($ch);
// Call `curl_close($ch)`
curl_close($ch);
// Print `"Request sent, response length: " . strlen($response ?: "")` to the output
echo "Request sent, response length: " . strlen($response ?: "");
?>

Response Headers Handle करना

CURLOPT_RETURNTRANSFER को true set करना cURL को बताता है कि response को page पर सीधे print करने के बजाय एक string की तरह वापस दे जिसे आपकी script inspect कर सके -- किसी API consume करते समय आमतौर पर यही चाहिए होता है।

उदाहरण: Handling Response Headers

php
<?php
// Declare `$ch`, set to `curl_init("https://example.com")`
$ch = curl_init("https://example.com");
// Call `curl_setopt($ch, CURLOPT_RETURNTRANSFER, true)`
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Declare `$response`, set to `curl_exec($ch)`
$response = curl_exec($ch);
// Call `curl_close($ch)`
curl_close($ch);
// Print "Response returned as a string instead of printed directly" to the output
echo "Response returned as a string instead of printed directly";
?>

POST Requests भेजना

एक POST request भेजने का मतलब है CURLOPT_POST को true set करना और CURLOPT_POSTFIELDS के through अपने form fields को एक array (या query string) की तरह pass करना, mirror करते हुए कि एक form submit करते समय browser क्या करता है।

उदाहरण: Sending POST Requests

php
<?php
// Declare `$ch`, set to `curl_init("https://example.com/api")`
$ch = curl_init("https://example.com/api");
// Call `curl_setopt($ch, CURLOPT_POST, true)`
curl_setopt($ch, CURLOPT_POST, true);
// Call `curl_setopt($ch, CURLOPT_POSTFIELDS, ["name" => "Alice"])`
curl_setopt($ch, CURLOPT_POSTFIELDS, ["name" => "Alice"]);
// Call `curl_setopt($ch, CURLOPT_RETURNTRANSFER, true)`
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Call `curl_exec($ch)`
curl_exec($ch);
// Call `curl_close($ch)`
curl_close($ch);
// Print "POST request sent with form fields" to the output
echo "POST request sent with form fields";
?>

cURL में Error Handling

Network calls कई कारणों से fail हो सकती हैं -- timeouts, DNS issues, refused connections -- इसलिए हर request के बाद curl_error() और curl_errno() check करना आपको उन failures को gracefully handle करने देता है बिना data के silently continue करने के।

उदाहरण: Error Handling in cURL

php
<?php
// Declare `$ch`, set to `curl_init("https://nonexistent.invalid")`
$ch = curl_init("https://nonexistent.invalid");
// Call `curl_setopt($ch, CURLOPT_RETURNTRANSFER, true)`
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Call `curl_setopt($ch, CURLOPT_TIMEOUT, 2)`
curl_setopt($ch, CURLOPT_TIMEOUT, 2);
// Declare `$response`, set to `curl_exec($ch)`
$response = curl_exec($ch);
// Check whether `curl_errno($ch)`
if (curl_errno($ch)) {
    // Print `"cURL error: " . curl_error($ch)` to the output
    echo "cURL error: " . curl_error($ch);
}
// Call `curl_close($ch)`
curl_close($ch);
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. CURLOPT_RETURNTRANSFER भूल जाना, ताकि curl_exec response को return करने के बजाय print कर दे।
  2. जब curl_exec false return करे तो curl_error check न करना।
  3. errors को गायब करने के लिए SSL verification disable करना, जो requests को insecure बना देता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.