Open In App

Why use Guzzle Instead of cURL in PHP ?

What is cURL? 
cURL is a module in PHP with we can use libcurl. The libcurl is a library that is used in PHP to create connection and communicate with various kinds of different servers which may have different type of protocols. cURl provide us various pre-built functions like – curl_init(), curl_setopt(), curl_exec(), curl_close().
Limitations of cURL: 
 

Example: These are the request made by using cURL.
 






<?php>
 
// Get cURL resource
$curl = curl_init();
// Set some options
curl_setopt($ch, CURLOPT_POST,
    "https://jsonplaceholder.typicode.com/users");
 
curl_setopt($ch, CURLOPT_POST, false) ;
 
curl_setopt($ch, CURLOPT_RETURNTANSFER, false) ;
$result = curl_exec($ch);
curl_close($ch);
 
?>

Output: 
 



What is Guzzle? 
Guzzle is a Microframework (abstraction layer) that is a PHP HTTP client due to which the HTTP request is sent easily and it is trivial to integrate with web services. Guzzle can be used with any HTTP handler like cURL, socket, PHP’s stream wrapper. Guzzle by default uses cURL as Http handler. 
Why use Guzzle Instead of cURL in PHP? 
 

Example: These are the request made by using Guzzle.
 




<?php
 
use GuzzleHTTP\Client;
require '>>/vendor/autoload.php';
 
$client = new Client([
    'base_uri'=>'http://httpbin.org',
    'timeout' => 2.0
]);
 
$response = $client->request('GET', 'ip');
 
echo $response->getStatusCOde(), "<br>";
$body = $response->getBody();
echo $body->getContents(), "<br>";
 
echo "<pre>";
print_r(get_class_methods($body));
echo "</pre>";
echo "<pre>";
print_r(get_class_methods($response));
echo "</pre>";
?>

Output: 
 

 


Article Tags :