PHP CURL GET and POST with Examples
In this article, you will learn about PHP cURL GET and POST with examples.
cURL stands for client URL, it is a tool for getting and sending data using various protocols, like GET, POST, FTP, COOKIES, SMTP and many more. It uses the libcurl library, which is portable and works identically on many different platforms. It also performs SSL certificate verification when dealing with HTTPs.
Before knowing, how to fetch data using curl. You should know about curl_setopt() curl function and their option's description.
The curl_setopt() function is used to set an option for a curl transfer. It returns a boolean value, i.e., either TRUE or FALSE.
Syntax of curl_setopt()
bool curl_setopt(resource $ch, int $option, mixed $value);
Here, $ch is a curl handler, $option specifies a curl option, and value specifies a value of the option. The complete list of options is available at the PHP official site.
http://php.net/manual/en/function.curl-setopt.phpIn this article, we are using the following options-
CURLOPT_URL- This contains the URL to fetch.
CURLOPT_RETURNTRANSFER- It is set to TRUE to transfer as a string of the return value of curl_exec().
CURLOPT_POST- It is set to true to post the data.
CURLOPT_POSTFIELDS- It contains the data to post in an HTTP POST operation.
Fetching data using cURL in PHP with GET Method
To fetch data from a curtain URL using the GET method is a common task. Here, we are using curl request to get the data. First, we initialise the curl using the curl_init() method. Then in the next line, we are using curl_setopt() method. We have passed three parameters to this method- curl initialization object, the CURLOPT_URL option to the URL, and specifying the URL as a value in the third parameter.
<?php
$c = curl_init();
curl_setopt($c, CURLOPT_URL,'https://www.etutorialspoint.com/distancecalculator.php');
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
echo $page = curl_exec($c);
if (curl_errno($c)) {
$error_msg = curl_error($c);
}
curl_close($c);
?>
Sending data using cURL in PHP with POST Method
Here, we have sent data to a PHP page using the curl POST method in PHP. Here, we have first initialized the curl using curl_init() method. Next, we have used the curl_setopt() method to set the POST request to the 'distancecalculator.php' page. With the help of the CURLOPT_POSTFIELDS option, we have set the sending post variables.
<?php
$body = 'source=rkpuram&dest=vasali';
$c = curl_init();
curl_setopt($c, CURLOPT_URL,'https://www.etutorialspoint.com/distancecalculator.php');
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, $body);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
$page = curl_exec($c);
echo $page
if(curl_errno($c)) {
$error_msg = curl_error($c);
echo $error_msg
}
curl_close($c);
?>
Related Articles
PHP sanitize input for MySQLPHP random quote generator
PHP String Contains
PHP calculate percentage of total
PHP Fix: invalid argument supplied for foreach
Locking files with flock()
How to Pass an Array as URL Parameter in PHP
How to generate pdf in PHP using MySQL and MPDF Library
Set and Get Cookies in PHP
Hypertext Transfer Protocol Overview
Simple PHP File Cache
PHP Server Side Form Validation
How to upload multiple files and store in MySQL database using PHP
Create And Download Word Document in PHP
How to display PDF file in PHP from database
How to read CSV file in PHP and store in MySQL
Create And Download Word Document in PHP
PHP SplFileObject Standard Library