What is the cURL in PHP?

Updated on ... 10th February 2023

The PHP cURL stands for ‘Client for URLs’, cURL is a PHP inbuilt library that is the most powerful extension. It allows the user to create the HTTP requests in PHP. Think about API (API works like a mediator between two servers), so we need a library while creating an API for communicating between two servers through URL.

The PHP cURL library is used to communicate one server to other servers with the help of protocols.

In simple words cURL allows us to send and receive the data through the URL syntax. cURL makes it easy to communicate between different websites and domains.

Tree directory structure  script With PHP

Some most usable cURL Functions:

S. No Function Name Description
1. curl_init() Function will start a new session and return a cURL handle
2. curl_setopt() This function defines a various option for the cURL session.
3. curl_setopt($ch, option, value) This function is used to set an option for a cURl session and the value for a cURL session by the "ch" parameter.
3. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) This function return page contents. If set false or 0 then no output will be returned.
3. curl_setopt($ch, CURLOPT_URL, $url) This function pass URL as a parameter. $url is our target server web address. in simple words this is the URL that we want to get from the internet
4. curl_exec() This function executes the cURL session, and it should be start after initializing a curl and all the options are set for the session, It's actual use to execute the predefined cURL session (defined by ch)
5. curl_close() This function is used to close curl resource and free up system resources.
Note: Don't foget to use  curl_close($ch);


Because if you don't use it - on Windows this will produce a windows-error-report (Program terminated unexpectedly)

Example 01: 

                                                
                                                
                                                <?php
$url = "https://bootstrapfriendly.com/about/";
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);

echo $result;
?>
                                                
                                            

The output of the above code will be:


Leave a comment