Curl GET And POST Method Calls - PHP Example

Curl is an open source command line tool and library for transferring data with URL.
curl is a powerful system to transfer data to many protocals. it is a good way to send data between websites.
cURL is unrestricted so it can be make simple HTTP Request, also make complex FTP upload with an authentication, also can make HTTPS requests.

Steps to make CURL calls :

Step1:

We will initialize the curl by calling curl_init() function. After this initation we can call curl resources functions.

Step2:

There are many setting that we can use for different works with curl_setopt() function.

Ex : bool curl_setopt ( $curl_object , $option , mixed $value );

First parameter $curl_object
A cURL handle returned by curl_init().

Second parameter is setting $option
The CURLOPT_XXX option to set.

Third  parameter is $value
The value to be set on option. ex. array or any value

Below are the some main settings we can use inside curl_setopt() function
A. CURLOPT_RETURNTRANSFER – Set it TRUE to return the response as a string, if not set this parameter then response will outputting on page then we can use file_get_contents(‘url’) to get response. 
Ex. curl_setopt($curl_object, CURLOPT_RETURNTRANSFER, true);

B. CURLOPT_CONNECTTIMEOUT – Number of seconds to try to connect 
Ex. curl_setopt($curl_object, CURLOPT_CONNECTTIMEOUT, 60);

C. CURLOPT_URL – URL to sent curl request.
Ex. curl_setopt($curl_object, CURLOPT_URL, “http://www.curlexample.com”);

D. CURLOPT_POST – Set when sending curl post request. 
Ex. curl_setopt($curl_object, CURLOPT_POST, true);

E. CURLOPT_POSTFIELDS – Array of data sent in post.
Ex. curl_setopt($curl_object, CURLOPT_POST, array(“querystring1” => “parameter1”, “querystring2” => “parameter1”)); F.CURLOPT_TIMEOUT – Number of seconds cURL will take to execute.
Ex. curl_setopt($curl_object, CURLOPT_TIMEOUT, 60);

G. CURLOPT_FRESH_CONNECT – Each curl call be refresh. 
Ex. curl_setopt($curl_object, CURLOPT_TIMEOUT, true);

H. CURLOPT_USERPWD – Some pages/api required authentication to access, for these cases we will use this setting. 
Ex. curl_setopt($crl, CURLOPT_USERPWD, “Username:Password”);

Step3:

curl_exec() call will execute for curl settings and return response.

Step4:

curl_close() will call to free all curl resources.

 

==> MAKE GET METHOD REQUEST

Making a GET method call by curl.


<?php

		$url = 'https://curlbasics.com?curl=basic&method=get';
		
		$crl = curl_init();
		curl_setopt($crl, CURLOPT_URL, $url);
		curl_setopt($crl, CURLOPT_FRESH_CONNECT, true);
		curl_setopt($crl, CURLOPT_RETURNTRANSFER, true);
		
		$response = curl_exec($crl);
		if(!response){
		   die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
		}
		
		curl_close($crl);
		
?>

==> MAKE POST METHOD REQUEST

Making a POST method call by curl.


<?php 

	  $data = array(
		        "query1" => "parameter1 ",
		        "text" => "Hi, Message from android example"
	          );

	   $url = "https://curl-post-call.com";

	   $ch = curl_init ($url);
	   curl_setopt ($ch, CURLOPT_POST, true);
	   curl_setopt ($ch, CURLOPT_POSTFIELDS, $data);
	   curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);

	   $response = curl_exec ($ch); 
	   if(!response){
			   die("Error: "" . curl_error($ch) . "" - Code: " . curl_errno($ch));
		 }

	   curl_close($crl);

?>

==> MAKE POST METHOD REQUEST WHERE AUTHENTICATION REQUIRED

Making a POST method call by curl.


<?php 

		   $AUTH_USERNAME = "Username";
		   $AUTH_PASSWORD = "Password";

		   $url = "https://authenticated-curl-call.com/";
		   $data = array("src" => "source",  "text" => "test curl request");

		   $ch=curl_init($url);
		   curl_setopt($ch, CURLOPT_POST, true);
		   curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
		   curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);

		   curl_setopt($ch, CURLOPT_USERPWD, $AUTH_USERNAME . ":" . $AUTH_PASSWORD);

		   $response = curl_exec( $ch );
		   if(!response){
			   die("Error: "" . curl_error($ch) . "" - Code: " . curl_errno($ch));
		   }

		   curl_close($ch);

?> 

==> MAKE POST METHOD REQUEST WHERE AUTHENTICATION REQUIRED POST REQUEST AS JSON

Making a POST method json call by curl.


<?php 

			$AUTH_USERNAME = "Username";
			$AUTH_PASSWORD = "Password";

			$url = "https://curl-json-call.com/";
			$data = array("src" => "source",  "text" => "test curl request");
			$data_string = json_encode($data);

			$ch=curl_init($url);

			curl_setopt($ch, CURLOPT_POST, true);

			curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

			curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);

			curl_setopt($ch, CURLOPT_USERPWD, $AUTH_USERNAME . ":" . $AUTH_PASSWORD);

			curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));

						
			$response = curl_exec( $ch );
			if(!response){

				die("Error: "" . curl_error($ch) . "" - Code: " . curl_errno($ch));

			}

			curl_close($ch);

?>