php post vs get code example
Example 1: post request php
$response = httpPost("http://mywebsite.com/update.php",
array("first_name"=>"Bob","last_name"=>"Dillon")
);
//using php curl (sudo apt-get install php-curl)
function httpPost($url, $data){
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
Example 2: difference between GET, POST and REQUEST methods?
GET and POST are used to send information from client browser to web server.
GET the information is send via GET method in name/value pair and is URL encoded.
The default GET has a limit of 512 characters.
The POST method transfers the information via HTTP Headers.
The POST method does not have any restriction in data size to be sent.
POST is used for sending data securely and ASCII and binary type's data.
The $_REQUEST contains the content of both $_GET, $_POST and $_COOKIE.
Example 3: http response methods
resource HTTP response method is made up of three
components:
Response Status Code ==> 200, 301, 404, 500
(these are the most common ones)
Response Header Fields ==> Date, Server, LastModified, Content-Type
Response Body ==> This is the data that comes
back to the client from the server.