PHP cURL is a function in PHP that makes it possible for PHP programs to use libcurl library.
libcurl is a library created by Daniel Stenberg that allows you to communicate to diffrenet type of servers with many different types of protocols.
libcurl currently supports the http,https,telnet,ftp,gopher,file and ldap protocols.
Using cURL we can do below things:
Calling Rest API
Sending POST request.
Sending Receiving XML
Passing Custom HTTP header.
PHP cURL uses the following sequence of steps.
curl_init: Initialize a curl session and return a handle which can be used by other cURL functions.
curl_setopt: Set various options for the cURL session handle
curl_exec: Executes a cURL session.
curl_close: Closes the current cURL session and frees all resources. The cURL handle, ch, is also deleted.
Following code returns the curl output as a string.
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, “example.com”);
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources