Get post request method in php

PHP GET/POST request

PHP GET/POST request tutorial shows how to generate and process GET and POST requests in PHP. We use plain PHP and Symfony, Slim, and Laravel frameworks.

$ php -v php -v PHP 8.1.2 (cli) (built: Aug 8 2022 07:28:23) (NTS) .

HTTP

The is an application protocol for distributed, collaborative, hypermedia information systems. HTTP protocol is the foundation of data communication for the World Wide Web.

HTTP GET

The HTTP GET method requests a representation of the specified resource.

  • should only be used to request a resource
  • parameters are displayed in the URL
  • can be cached
  • remain in the browser history
  • can be bookmarked
  • should never be used when dealing with sensitive data
  • have length limits

HTTP POST

The HTTP POST method sends data to the server. It is often used when uploading a file or when submitting a completed web form.

  • should be used to create a resource
  • parameters are not displayed in the URL
  • are never cached
  • do not remain in the browser history
  • cannot be bookmarked
  • can be used when dealing with sensitive data
  • have no length limits

PHP $_GET and $_POST

PHP provides the $_GET and $_POST superglobals. The $_GET is an associative array of variables passed to the current script via the URL parameters (query string). The $_POST is an associative array of variables passed to the current script via the HTTP POST method when using application/x-www-form-urlencoded or multipart/form-data as the HTTP Content-Type in the request.

PHP GET request

In the following example, we generate a GET request with curl tool and process the request in plain PHP.

 $message = $_GET['message']; if ($message == null) < $message = 'hello there'; >echo "$name says: $message";

The example retrieves the name and message parameters from the $_GET variable.

$ php -S localhost:8000 get_req.php
$ curl 'localhost:8000/?name=Lucia&message=Cau' Lucia says: Cau $ curl 'localhost:8000/?name=Lucia' Lucia says: hello there

We send two GET requests with curl.

PHP POST request

In the following example, we generate a POST request with curl tool and process the request in plain PHP.

 $message = $_POST['message']; if ($message == null) < $message = 'hello there'; >echo "$name says: $message";

The example retrieves the name and message parameters from the $_POST variable.

$ php -S localhost:8000 post_req.php
$ curl -d "name=Lucia&message=Cau" localhost:8000 Lucia says: Cau

We send a POST request with curl.

Читайте также:  Python scipy optimize fsolve

PHP send GET request with Symfony HttpClient

Symfony provides the HttpClient component which enables us to create HTTP requests in PHP.

$ composer req symfony/http-client

We install the symfony/http-client component.

request('GET', 'http://localhost:8000', [ 'query' => [ 'name' => 'Lucia', 'message' => 'Cau', ] ]); $content = $response->getContent(); echo $content . "\n";

The example sends a GET request with two query parameters to localhost:8000/get_request.php .

$ php -S localhost:8000 get_req.php
$ php send_get_req.php Lucia says: Cau

We run the send_get_req.php script.

PHP send POST request with Symfony HttpClient

In the following example, we send a POST request with Symfony HttpClient.

request('POST', 'http://localhost:8000', [ 'body' => [ 'name' => 'Lucia', 'message' => 'Cau', ] ]); $content = $response->getContent(); echo $content . "\n";

The example sends a POST request with two parameters to localhost:8000/post_req.php .

$ php -S localhost:8000 post_req.php
$ php send_post_req.php Lucia says: Cau

We run the send_post_req.php script.

PHP GET request in Symfony

In the following example, we process a GET request in a Symfony application.

$ symfony new symreq $ cd symreq

A new application is created.

$ composer req annot $ composer req maker --dev

We install the annot and maker components.

$ php bin/console make:controller HomeController

We create a new controller.

) */ public function index(Request $request): Response < $name = $request->query->get('name', 'guest'); $message = $request->query->get('message', 'hello there'); $output = "$name says: $message"; return new Response($output, Response::HTTP_OK, ['content-type' => 'text/plain']); > >

Inside the HomeController’s index method, we get the query parameters and create a response.

$name = $request->query->get('name', 'guest');

The GET parameter is retrieved with $request->query->get . The second parameter of the method is a default value which is used when no value was retrieved.

$ curl 'localhost:8000/?name=Lucia&message=Cau' Lucia says: Cau

We generate a GET request with curl.

PHP POST request in Symfony

In the following example, we process a POST request in a Symfony application.

) */ public function index(Request $request): Response < $name = $request->request->get('name', 'guest'); $message = $request->request->get('message', 'hello there'); $output = "$name says: $message"; return new Response($output, Response::HTTP_OK, ['content-type' => 'text/plain']); > >

We change the controller to process the POST request.

$name = $request->request->get('name', 'guest');

The POST parameter is retrieved with $request->request->get . The second parameter of the method is a default value which is used when no value was retrieved.

$ curl -d "name=Lucia" localhost:8000 Lucia says: hello there

We generate a POST request with curl.

PHP GET request in Slim

In the following example, we are going to process a GET request in the Slim framework.

$ composer req slim/slim $ composer req slim/psr7 $ composer req slim/http

We install slim/slim , slim/psr7 , and slim/http packages.

get('/', function (Request $request, Response $response): Response < $name = $request->getQueryParam('name', 'guest'); $message = $request->getQueryParam('message', 'hello there'); $output = "$name says $message"; $response->getBody()->write($output); return $response; >); $app->run();

We get the parameters and return a response in Slim.

$name = $request->getQueryParam('name', 'guest');

The query parameter is retrieved with getQueryParam ; the second parameter is the default value.

$response->getBody()->write($output);

We write the output to the response body with write .

$ php -S localhost:8000 -t public
$ curl 'localhost:8000/?name=Lucia&message=Cau' Lucia says: Cau

We generate a GET request with curl.

Читайте также:  Java hashmap value class

PHP POST request in Slim

In the following example, we are going to process a POST request in the Slim framework.

post('/', function (Request $request, Response $response): Response < $data = $request->getParsedBody(); $name = $data['name']; $message = $data['message']; if ($name == null) < $name = 'guest'; >if ($message == null) < $message = 'hello there'; >$output = "$name says: $message"; $response->getBody()->write($output); return $response; >); $app->run();

We get the POST parameters and return a response in Slim.

$data = $request->getParsedBody();

The POST parameters are retrieved with getParsedBody .

$ php -S localhost:8000 -t public
$ curl -d "name=Lucia" localhost:8000 Lucia says: hello there

We generate a POST request with curl.

PHP GET request in Laravel

In the following example, we process a GET request in Laravel.

$ laravel new larareq $ cd larareq

We create a new Laravel application.

query('name', 'guest'); $message = $request->query('message', 'hello there'); $output = "$name says $message"; return $output; >);

We get the GET parameters and create a response.

$ curl 'localhost:8000/?name=Lucia&message=Cau' Lucia says Cau

We send a GET request with curl.

PHP POST request in Laravel

In the following example, we send a POST request from an HTML form.

We have a POST form in a Blade template. Laravel requires CSRF protection for POST requests. We enable CSRF protection with @csrf .

); Route::post('/process_form', function (Request $request) < $request->validate([ 'name' => 'required|min:2', 'message' => 'required|min:3' ]); $name = $request->input('name'); $message = $request->input('message'); $output = "$name says: $message"; return $output; >);

We validate and retrieve the POST parameters and send them in the response. This example should be tested in a browser.

In this tutorial, we have worked with GET and POST requests in plain PHP, Symfony, Slim, and Laravel.

Источник

PHP — GET & POST Methods

There are two ways the browser client can send information to the web server.

Before the browser sends the information, it encodes it using a scheme called URL encoding. In this scheme, name/value pairs are joined with equal signs and different pairs are separated by the ampersand.

name1=value1&name2=value2&name3=value3

Spaces are removed and replaced with the + character and any other nonalphanumeric characters are replaced with a hexadecimal values. After the information is encoded it is sent to the server.

The GET Method

The GET method sends the encoded user information appended to the page request. The page and the encoded information are separated by the ? character.

http://www.test.com/index.htm?name1=value1&name2=value2
  • The GET method produces a long string that appears in your server logs, in the browser’s Location: box.
  • The GET method is restricted to send upto 1024 characters only.
  • Never use GET method if you have password or other sensitive information to be sent to the server.
  • GET can’t be used to send binary data, like images or word documents, to the server.
  • The data sent by GET method can be accessed using QUERY_STRING environment variable.
  • The PHP provides $_GET associative array to access all the sent information using GET method.
Читайте также:  Cpp std terminate it

Try out following example by putting the source code in test.php script.

"; echo "You are ". $_GET['age']. " years old."; exit(); > ?>  
Name: Age:

It will produce the following result −

Forms

The POST Method

The POST method transfers information via HTTP headers. The information is encoded as described in case of GET method and put into a header called QUERY_STRING.

  • The POST method does not have any restriction on data size to be sent.
  • The POST method can be used to send ASCII as well as binary data.
  • The data sent by POST method goes through HTTP header so security depends on HTTP protocol. By using Secure HTTP you can make sure that your information is secure.
  • The PHP provides $_POST associative array to access all the sent information using POST method.

Try out following example by putting the source code in test.php script.

 echo "Welcome ". $_POST['name']. "
"; echo "You are ". $_POST['age']. " years old."; exit(); > ?>
Name: Age:

It will produce the following result −

Forms

The $_REQUEST variable

The PHP $_REQUEST variable contains the contents of both $_GET, $_POST, and $_COOKIE. We will discuss $_COOKIE variable when we will explain about cookies.

The PHP $_REQUEST variable can be used to get the result from form data sent with both the GET and POST methods.

Try out following example by putting the source code in test.php script.

"; echo "You are ". $_REQUEST['age']. " years old."; exit(); > ?>  
Name: Age:

Here $_PHP_SELF variable contains the name of self script in which it is being called.

It will produce the following result −

Источник

Get post request method in php

Learn Latest Tutorials

Splunk tutorial

SPSS tutorial

Swagger tutorial

T-SQL tutorial

Tumblr tutorial

React tutorial

Regex tutorial

Reinforcement learning tutorial

R Programming tutorial

RxJS tutorial

React Native tutorial

Python Design Patterns

Python Pillow tutorial

Python Turtle tutorial

Keras tutorial

Preparation

Aptitude

Logical Reasoning

Verbal Ability

Company Interview Questions

Artificial Intelligence

AWS Tutorial

Selenium tutorial

Cloud Computing

Hadoop tutorial

ReactJS Tutorial

Data Science Tutorial

Angular 7 Tutorial

Blockchain Tutorial

Git Tutorial

Machine Learning Tutorial

DevOps Tutorial

B.Tech / MCA

DBMS tutorial

Data Structures tutorial

DAA tutorial

Operating System

Computer Network tutorial

Compiler Design tutorial

Computer Organization and Architecture

Discrete Mathematics Tutorial

Ethical Hacking

Computer Graphics Tutorial

Software Engineering

html tutorial

Cyber Security tutorial

Automata Tutorial

C Language tutorial

C++ tutorial

Java tutorial

.Net Framework tutorial

Python tutorial

List of Programs

Control Systems tutorial

Data Mining Tutorial

Data Warehouse Tutorial

Javatpoint Services

JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.

  • Website Designing
  • Website Development
  • Java Development
  • PHP Development
  • WordPress
  • Graphic Designing
  • Logo
  • Digital Marketing
  • On Page and Off Page SEO
  • PPC
  • Content Development
  • Corporate Training
  • Classroom and Online Training
  • Data Entry

Training For College Campus

JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week

Like/Subscribe us for latest updates or newsletter RSS Feed Subscribe to Get Email Alerts Facebook Page Twitter Page YouTube Blog Page

Источник

Оцените статью