Tech Nuske

Tech Nuske

Monday, 16 April 2012

REST in PHP

(cont.)

In previous post we had learned the basic methods of cURL to send the GET request.
Today we  will see how to send those data using POST method.
GET method is no good for the large data because it is send in the url and it is visible to all. It's size is also limited so you can't send large data using the GET method.


In POST the data is bind in the data segment of the HTTP request packet. So it is not visible to other and only rendered by client.


To implement this in PHP we can use some cURL option.


Example.
view.php
<html>
<body>
<form name="form1" method="post" action="send name.php">
First Name  = <input type="text" name="txtfname" id="txtfname">
Last Name  = <input type="text" name="txtlname" id="txtlname">
<input type="submit" value="submit" name="submit">
</form>
</body>
</html>


send name.php


<?php
//extracting data
$firstname=$_POST['txtfname'];
$lastname=$_POST['txtlname']; 


//set URL
$url = "www.example.com";


//data
$data = "firstname=".$firstname."&lastname=".$lastname;


//Initialize and Set option for cURL object
$pst = curl_init();
$pst = curl_setopt($pst,CURLOPT_URL,$url);
$pst = curl_setopt($pst,CURLOPT_POST,true);
$pst = curl_setopt($pst,CURLOPT_RETURNTRANSFER,true);
$pst = curl_setopt($pst,CURLOPT_POSTFIELDS,$data);


//execute cURL objext
$result = curl_exec();


//close cURL object
curl_close($pst);


echo $result;


?>
Using this example you can send data to any other web server having different platforms.
change code as per your requirement and enjoy.
there is no limit how much data you can send using POST method.


By Nanji Parmar

No comments:

Post a Comment