Today, We want to share with you PHP CURL POST and GET REQUEST Methods.In this post we will show you PHP CURL POST and GET REQUEST Examples, hear for Execute a HTTP POST Using PHP CURL we will give you demo and example for implement.
In this post, we will learn about PHP CURL POST & GET Examples – Submit Form using PHP CURL with an example.

PHP CURL POST and GET Examples

What is curl?

curl stands for simple Client URL. cURL is a one type of library to transfer data via Following protocol like http, ftp, tftp etc. Now By using cURL we can send HTTP request(server side) using Following method like PUT,DELETE, GET, POST etc.

cURL overview

cURL allows transfer of data across a wide variety of protocols, and is a very powerful system.
First of all The first cURL library(Requestd client to server) was released in 1997.
It’s widely used as a way to (client side to server side)send data across websites(like api), so including things like (Web-services)API interaction(JSON or xml) and oAuth(HTTP header).
and other used to The command line is designed to work send request without user interaction.
The curl main fetures of all transfer-related data.(like images,text,data,etc.).
curl is a tool to (client side to server side data transfer)transfer data from or to a server, It’s using one of the following list of supported protocols Like.(HTTP, HTTPS, FTP, FTPS, GOPHER, DICT, TELNET, LDAP or FILE).

Read Also:  PHP Laravel CURL HTTP GET POST Request

curl − Data transfer a URL

The following List of cURL Function.

  • first : curl_init
  • Second : curl_setopt & curl_setopt_array
  • Third : curl_exec //will return true or false
  • Fourth : curl_getinfo
  • Fifth : curl_close
  • Six : curl_error //Function return last error of the curl_session. If any error will not get then it will return string is empty.
  • Seven : curl_errorno


curl_init(); used to Initialize CURL session function

curl Setting

1.CURLOPT_RETURNTRANSFER – setting option Return Transfer
or Return the response as a string instead of outputting it to the screen
2.CURLOPT_CONNECTTIMEOUT – Number of seconds to spend attempting to connect
3.CURLOPT_TIMEOUT – Number of seconds to allow cURL to execute
4.CURLOPT_USERAGENT – Useragent string to use for request
5.CURLOPT_URL – Setting option url or URL to send request to
6.CURLOPT_POST – Send request as POST
7.CURLOPT_POSTFIELDS – Array of data to POST in request
8.curl_exec() //will return true or false or method which will execute the cURL request.

other curl using function

I) curl_close()
II) curl Errors
III) curl_error() -Function return last error of the curl_session. If error will not occur then it will return empty string.
IV) curl_errno() -Function return last error number of curl_session

Read Also:  PHP cURL Basic CRUD Example

curl using GET Request Example

	$apiKey='X-Authrocket-Api-Key: ko_0vJ3PMz6555DGFDGLIGWqx-VF4C2CTPAmmk8oolmFKFIHHdFFrNRKleikc37uIr';
	//Your Application API KEY
	$Realm='X-Authrocket-Realm: rl_0vJ3SJYUIP2Q888881XBCvXKV';//Your Application SECRATE KEY

    $ch = curl_init();  
    curl_setopt($ch,CURLOPT_URL,$url); //It's Setting option url
	curl_setopt($ch, CURLOPT_HTTPHEADER, array($apiKey,$Realm,//apikey and secrate key
	'Accept: application/json',//headers
	'Content-type: application/x-www-form-urlenphpd'
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);//setting option Return Transfer
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $output=curl_exec($ch); //will return true or false
    if($output === false)
        echo "Function return last error number of curl_session : Error Number:".curl_errno($ch)."
        echo " Function return last error number of curl_session Error String:".curl_error($ch);

	$obj = json_dephp($output, TRUE);
	for($i=0; $i&<count($obj['collection']); $i++) { ?&>
		//start table row

		//Table data 

&<td&>&<?php echo $obj['collection'][$i]['object']; ?&>&</td&>


&<td&>&<a href="index.php?q=&<?php echo $obj['collection'][$i]['id']; ?&>"&>DELETE record&</a&>&</td&>

		//End Table data 

		//End table row

		&<?php } ?&>

curl using POST Request Example

&<?php $apiKey='X-Authrocket-Api-Key: ko_0vJ3PMz6y4maIK9IFn4Wqx-VF4C2CTPAmmk8oolm6SsKeW21tdFFrNRKleikc37uIr'; $Realm='X-Authrocket-Realm: rl_0vJ3S0FbRzFo2Q1XBCvXKV'; if(isset($_POST['submit'])) { //Parameter List $appfname=$_POST['appfname'];//First Name $userlname=$_POST['userlname']; $clientemail=$_POST['clientemail']; // Email Address $strongpass=$_POST['strongpass']; $utype="human"; // Usertype like human,robots //initialize array $parameters=array(); //using class class Emp { public $username = ""; public $email = ""; public $password = ""; public $user_type = ""; public $state=""; } // pass parameters $e = new Emp(); $e-&>username = $fname;
   $e-&>email  = $email;
   $e-&>password = $pass;


    $ch = curl_init();  
    curl_setopt($ch,CURLOPT_URL,$url);//It's Setting option url
	curl_setopt($ch, CURLOPT_POST,1);//make our session prepare to send data over HTTP post 
	curl_setopt($ch, CURLOPT_POSTFIELDS,$parameters);
	curl_setopt($ch, CURLOPT_HTTPHEADER, array($apiKey,$Realm,
	'Content-type: application/json'
	//header pass in parameters
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);//setting option Return Transfer
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $output=curl_exec($ch); //will return true or false
    if($output === false)
        echo "Error Number Function return last error number of curl_session:".curl_errno($ch)."
        echo "Error String Function return last error number of curl_session:".curl_error($ch);
		$msg="created user Successfully users.";
		header("Location: index.php?msg=$msg");
		echo "
		echo "&</pre&>




Retrieves Images using curl

$image_url =";
$ch = curl_init();
$timeout = 0; // timeout initialize
curl_setopt ($ch, CURLOPT_URL, $image_url);// It's Setting option url
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

// all data are Getting binary data
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//setting option Return Transfer
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);//boolean true or false return and check

$image = curl_exec($ch); //will return true or false
curl_close($ch);//close connection

// all images dsiplay or output to browser
header("Content-type: image/jpeg");//like png,gif.etc.
echo "
print $image;
echo "&</pre&>


curl Sending Multiple Requests

&<?php // URLs we want to retrieve data using curl $urls_list = array( '', '', '', '', '' ); // initialize the multihandler $ng = curl_multi_init(); $list_channels = array(); foreach ($urls_list as $key =&> $url_data) {
    // initiate individual list_channels
    $list_channels[$key] = curl_init();
    curl_setopt_array($list_channels[$key], array(
        CURLOPT_URL =&> $url_data,//It's Setting option url_data
        CURLOPT_RETURNTRANSFER =&> true,//setting option Return Transfer

    // add list_channels to multihandler
    curl_multi_add_handle($ng, $list_channels[$key]);

// now execute -so if there is an active mode  connection then keep looping users.
$active = null;
do {
    $status_data = curl_multi_exec($ng, $active);

while ($active &amp;&amp; $status_data == CURLM_OK);

// display the content, remove the specific handlers, and then close them
foreach ($list_channels as $ng_chan) {
    echo curl_multi_getcontent($ng_chan);
    curl_multi_remove_handle($ng, $ng_chan);

// Last close the multihandler end