PHP cURL Introduction
In this page:
$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
// 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");
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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 ?: "");
?>
Login to try C/C++/Java/PHP code in the editor
Response Headers Handle करना
CURLOPT_RETURNTRANSFER को true set करना cURL को बताता है कि response को page पर सीधे print करने के बजाय एक string की तरह वापस दे जिसे आपकी script inspect कर सके -- किसी API consume करते समय आमतौर पर यही चाहिए होता है।
उदाहरण: Handling Response Headers
<?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";
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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";
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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);
?>
Login to try C/C++/Java/PHP code in the editor
CURLOPT_RETURNTRANSFERभूल जाना, ताकिcurl_execresponse को return करने के बजाय print कर दे।- जब
curl_execfalse return करे तोcurl_errorcheck न करना। - errors को गायब करने के लिए SSL verification disable करना, जो requests को insecure बना देता है।
Chapter Quiz — Complete all 24 topics to unlock
0/24 topics done
Complete these topics first:
- PHP Date & Time
- PHP Math Functions
- PHP JSON Handling
- PHP XML Handling
- PHP cURL Introduction
- PHP REST API Basics
- PHP Composer & Packages
- PHP Autoloading
- PHP Design Patterns
- PHP MVC Architecture
- PHP Security Best Practices
- PHP Performance Optimization
- PHP 8 New Features
- PHP Type Declarations
- PHP Match Expression Advanced
- PHP Fibers
- PHP Attributes
- PHP Magic Constants
- PHP Include & Require
- PHP Iterables
- PHP SimpleXML Parser
- PHP SimpleXML Get
- PHP XML Expat Parser
- PHP DOM Parser