PHP CURL POST and GET REQUEST Methods

PHP CURL POST and GET REQUEST Methods

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).

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

curl using GET Request Example

http://testcURL.com/?item1=value&item2=value2.


		//start table row

&
		//Table data 

&&&

				........
				..........
				.........
				.........
				........
				........

&&DELETE record&&

		//End Table data 
		&

		//End table row

		&

curl using POST Request Example

&username = $fname;
   $e-&>email  = $email;
   $e-&>password = $pass;
   $e-&>user_type=$utype;
   $e-&>state="active";

	$parameters['user']=$e;
	$parameters=json_enphp($parameters);
	
	$url="https://www.pakainfo.com/v1/users";

    $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);
    }
	else
	{
		$msg="created user Successfully users.";
		header("Location: index.php?msg=$msg");
		echo "
&";
		print_r($output);
		echo "&

";
	}
    curl_close($ch);

}

?&>

Retrieves Images using curl

&";
print $image;
echo "&

";
?&>

curl Sending Multiple Requests


& $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
        CURLOPT_FOLLOWLOCATION =&> true
    ));

    // 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);
    curl_close($ng_chan);
}

// Last close the multihandler end
curl_multi_close($ng);

Leave a Comment