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 permintaanDalam 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.phpKami 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: CauKami 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-clientKami 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
Kami memulai server
$ php send_get_req.php Lucia says: CauKami 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 0Kami memulai server
The example retrieves the name and message parameters from the $_GET variable.
$ php -S localhost:8000 get_req.php 1Kami 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 2Sebuah aplikasi baru dibuat
The example retrieves the name and message parameters from the $_GET variable.
$ php -S localhost:8000 get_req.php 3Kami 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 4Kami 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 5Di 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 6Parameter 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.
Kami memulai server
The example retrieves the name and message parameters from the $_GET variable.
$ php -S localhost:8000 get_req.php _8Kami 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 _9Kami 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 _0Parameter 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 7Kami memulai server
$ curl 'localhost:8000/?name=Lucia&message=Cau' Lucia says: Cau $ curl 'localhost:8000/?name=Lucia' Lucia says: hello there _2Kami 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 _3Kami 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 _4Kami 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 _5Parameter 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 _6Kami 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 _7Kami memulai server
The example retrieves the name and message parameters from the $_GET variable.
$ php -S localhost:8000 get_req.php _8Kami 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 _9Kami 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 _0Parameter 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 _7Kami memulai server
$ curl 'localhost:8000/?name=Lucia&message=Cau' Lucia says: Cau $ curl 'localhost:8000/?name=Lucia' Lucia says: hello there _2Kami 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 _3Kami membuat aplikasi Laravel baru
The example retrieves the name and message parameters from the $_POST variable.
$ php -S localhost:8000 post_req.php _4Kami 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 _5Kami memulai server
The example retrieves the name and message parameters from the $_POST variable.
$ php -S localhost:8000 post_req.php _6Kami 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 _7Kami 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 _8Kami 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