Bagaimana saya bisa mengakses data posting di php?

Tutorial permintaan GET/POST PHP menunjukkan cara membuat dan memproses permintaan GET dan POST di PHP. Kami menggunakan framework PHP dan Symfony, Slim, dan Laravel biasa

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

Kami menggunakan PHP versi 8. 1. 2

Hypertext Transfer Protocol (HTTP) adalah protokol aplikasi untuk sistem informasi terdistribusi, kolaboratif, hypermedia. Protokol HTTP adalah dasar dari komunikasi data untuk World Wide Web

DAPATKAN HTTP

Metode HTTP GET meminta representasi dari sumber daya yang ditentukan

DAPATKAN permintaan

  • seharusnya hanya digunakan untuk meminta sumber daya
  • parameter ditampilkan di URL
  • dapat di-cache
  • tetap ada di riwayat browser
  • dapat di-bookmark
  • tidak boleh digunakan saat menangani data sensitif
  • memiliki batas panjang

Metode HTTP POST mengirim data ke server. Ini sering digunakan saat mengunggah file atau saat mengirimkan formulir web yang sudah diisi

permintaan POST

  • harus digunakan untuk membuat sumber daya
  • parameter tidak ditampilkan di URL
  • tidak pernah di-cache
  • tidak tetap dalam riwayat browser
  • tidak dapat di-bookmark
  • dapat digunakan saat menangani data sensitif
  • tidak memiliki batas panjang

PHP $_GET dan $_POST

PHP menyediakan

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

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

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

$ php -S localhost:8000 post_req.php 9 adalah larik variabel asosiatif yang diteruskan ke skrip saat ini melalui parameter URL (string kueri). $ curl -d "name=Lucia&message=Cau" localhost:8000 Lucia says: Cau _0 adalah larik variabel asosiatif yang diteruskan ke skrip saat ini melalui metode HTTP POST saat menggunakan $ curl -d "name=Lucia&message=Cau" localhost:8000 Lucia says: Cau 3 atau $ curl -d "name=Lucia&message=Cau" localhost:8000 Lucia says: Cau 4 sebagai Jenis Konten HTTP dalam permintaan

Dalam contoh berikut, kami membuat permintaan GET dengan alat curl dan memproses permintaan tersebut dalam PHP biasa

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

$ php -S localhost:8000 get_req.php

Kami memulai server

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

Kami mengirim dua permintaan GET dengan curl

Permintaan PHP POST

Dalam contoh berikut, kami membuat permintaan POST dengan alat curl dan memproses permintaan tersebut dalam PHP biasa

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

$ php -S localhost:8000 post_req.php _

Kami memulai server

$ curl -d "name=Lucia&message=Cau" localhost:8000 Lucia says: Cau

Kami mengirim permintaan POST dengan curl

Symfony menyediakan komponen $ curl -d "name=Lucia&message=Cau" localhost:8000 Lucia says: Cau _5 yang memungkinkan kita membuat permintaan HTTP dalam PHP

$ composer req symfony/http-client

Kami memasang komponen $ curl -d "name=Lucia&message=Cau" localhost:8000 Lucia says: Cau _6

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

Contoh mengirimkan permintaan GET dengan dua parameter kueri ke $ curl -d "name=Lucia&message=Cau" localhost:8000 Lucia says: Cau 7

$ php -S localhost:8000 get_req.php

Kami memulai server

$ php send_get_req.php Lucia says: Cau

Kami menjalankan skrip $ curl -d "name=Lucia&message=Cau" localhost:8000 Lucia says: Cau _8

PHP mengirim permintaan POST dengan Symfony HttpClient

Dalam contoh berikut, kami mengirim permintaan POST dengan Symfony HttpClient

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

Contoh mengirim permintaan POST dengan dua parameter ke $ curl -d "name=Lucia&message=Cau" localhost:8000 Lucia says: Cau 9

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

$ php -S localhost:8000 get_req.php 0

Kami memulai server

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

$ php -S localhost:8000 get_req.php 1

Kami menjalankan skrip $ composer req symfony/http-client _0

Dalam contoh berikut, kami memproses permintaan GET di aplikasi Symfony

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

$ php -S localhost:8000 get_req.php 2

Sebuah aplikasi baru dibuat

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

$ php -S localhost:8000 get_req.php 3

Kami memasang komponen $ composer req symfony/http-client 1 dan $ composer req symfony/http-client 2

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

$ php -S localhost:8000 get_req.php 4

Kami membuat pengontrol baru

src/Controller/HomeController. php

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

$ php -S localhost:8000 get_req.php 5

Di dalam metode $ composer req symfony/http-client 3 $ composer req symfony/http-client 4, kita mendapatkan parameter kueri dan membuat respons

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

$ php -S localhost:8000 get_req.php 6

Parameter GET diambil dengan $ composer req symfony/http-client 5. Parameter kedua dari metode ini adalah nilai default yang digunakan saat tidak ada

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

$ php -S localhost:8000 get_req.php 7

Kami memulai server

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

$ php -S localhost:8000 get_req.php _8

Kami membuat permintaan GET dengan curl

Dalam contoh berikut, kami memproses permintaan POST di aplikasi Symfony

src/Controller/HomeController. php

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

$ php -S localhost:8000 get_req.php _9

Kami mengubah pengontrol untuk memproses permintaan POST

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

Parameter POST diambil dengan ________12______6. Parameter kedua dari metode ini adalah nilai default yang digunakan saat tidak ada

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

$ php -S localhost:8000 get_req.php 7

Kami memulai server

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

Kami membuat permintaan POST dengan curl

Dalam contoh berikut, kita akan memproses permintaan GET di framework Slim

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

Kami memasang paket $ composer req symfony/http-client 7, $ composer req symfony/http-client 8, dan $ composer req symfony/http-client 9

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

Kami mendapatkan parameter dan mengembalikan respons di Slim

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

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

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

Kami menulis output ke badan respons dengan request('GET', '//localhost:8000', [ 'query' => [ 'name' => 'Lucia', 'message' => 'Cau', ] ]); $content = $response->getContent(); echo $content . "\n"; 1

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

Kami memulai server

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

$ php -S localhost:8000 get_req.php _8

Kami membuat permintaan GET dengan curl

Dalam contoh berikut, kami akan memproses permintaan POST dalam kerangka Slim

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

Kami mendapatkan parameter POST dan mengembalikan respons di Slim

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

$ php -S localhost:8000 post_req.php _0

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

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

Kami memulai server

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

Kami membuat permintaan POST dengan curl

Permintaan PHP GET di Laravel

Dalam contoh berikut, kami memproses permintaan GET di Laravel

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

$ php -S localhost:8000 post_req.php _3

Kami membuat aplikasi Laravel baru

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

$ php -S localhost:8000 post_req.php _4

Kami mendapatkan parameter GET dan membuat respons

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

$ php -S localhost:8000 post_req.php _5

Kami memulai server

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

$ php -S localhost:8000 post_req.php _6

Kami mengirim permintaan GET dengan curl

Dalam contoh berikut, kami mengirim permintaan POST dari formulir HTML

sumber daya/tampilan/rumah. Pedang. php

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

$ php -S localhost:8000 post_req.php _7

Kami memiliki formulir POST di template Blade. Laravel membutuhkan perlindungan CSRF untuk permintaan POST. Kami mengaktifkan perlindungan CSRF dengan request('GET', '//localhost:8000', [ 'query' => [ 'name' => 'Lucia', 'message' => 'Cau', ] ]); $content = $response->getContent(); echo $content . "\n"; 3

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

$ php -S localhost:8000 post_req.php _8

Kami memvalidasi dan mengambil parameter POST dan mengirimkannya sebagai respons. Contoh ini harus diuji di browser

Dalam tutorial ini, kami telah bekerja dengan permintaan GET dan POST dalam PHP biasa, Symfony, Slim, dan Laravel

Bagaimana cara mendapatkan nilai dari metode posting di PHP?

Variabel $_REQUEST .

Bagaimana cara mendapatkan parameter POST di PHP?

Permintaan PHP POST . $message"; $name = $_POST['name']; if ($name == null) { $name = 'guest'; } $message = $_POST['message']; if ($message == null) { $message = 'hello there'; } echo "$name says: $message"; Contoh mengambil parameter nama dan pesan dari variabel $_POST. Kami memulai server.

Bagaimana cara mendapatkan data POST mentah di PHP?

Metode pilihan untuk mengakses data mentah POST adalah php. //masukan . $HTTP_RAW_POST_DATA tidak tersedia dengan enctype="multipart/form-data". Fitur ini TIDAK DIGUNAKAN LAGI di PHP 5. 6.

Bagaimana cara menggunakan metode posting di PHP?

Dalam metode PHP POST, data dari HTML FORM dikirim/dikumpulkan menggunakan variabel super global $_POST . Metode ini mengirimkan informasi yang disandikan yang disematkan di badan permintaan HTTP dan karenanya data tidak terlihat di URL halaman tidak seperti Metode GET.

Postingan terbaru

LIHAT SEMUA