PHP cURL

The cURL stands for ‘Client for URLs’ which has two library libcurl, curl and PHP/cURL.

libcurl library allows you to connect and communicate to many different types of servers with many different types of protocols. The libcurl currently supports the http, https, ftp, gopher, telnet, dict, file, and ldap protocols. The libcurl also supports HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, cookies, and user & password authentication.

curl is a command line tool for getting or sending files using URL syntax.

PHP/cURL is PHP module which makes it possible for PHP programs to use libcurl.

 

What is cURL in php?

The cURL is a PHP based library and a command line tool that helps you transfer data over FTP, TPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE, and LDAP. The cURL is used to get contents form another webpage in php.

How to use cURL in php?

We can use cURL in php program using below cURL functions.

  1. curl_init() : This function will initialize a new session and return a cURL handle $ch.
  2. curl_setopt($ch, option, value) : This function set value for an option for a cURL session identified by the ch parameter.  It return page contents if you set CURLOPT_RETURNTRANSFER option value is 1. If CURLOPT_RETURNTRANSFER set 0 then no output will be returned. CURLOPT_URL option pass URL as a parameter for your targeted server website address where you want to get web contents.
  3. curl_exec($ch) : This function execute the curl session given by $ch. It returns output.
  4. curl_close($ch) : close curl session and free up system resources.

We can understand cURL using below example.

Example :

<?php
//code by aryatechno
// Specify URL to get web page contents.
$url = "https://www.aryatechno.com/curl.php";

// Initialize a cURL session. It returns cURL handler
$ch = curl_init();

// Set CURLOPT_RETURNTRANSFER = 1 to return page contents.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

//Pass URL to the variable $url.
curl_setopt($ch, CURLOPT_URL, $url);

//Execute cURL session and ger page contents
$result = curl_exec($ch);
echo $result;

//Close cURL session
curl_close($ch)


?>

Output :

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

50823