Hướng dẫn yêu cầu GET/POST trong PHP chỉ ra cách tạo và xử lý các yêu cầu GET và POST trong PHP. Chúng tôi sử dụng các khung công tác PHP và Symfony, Slim và Laravel đơn giản
$ php -v php -v PHP 8.1.2 [cli] [built: Aug 8 2022 07:28:23] [NTS] ...
Chúng tôi sử dụng phiên bản PHP 8. 1. 2
Giao thức truyền tải siêu văn bản [HTTP] là một giao thức ứng dụng cho các hệ thống thông tin siêu phương tiện, cộng tác, phân tán. Giao thức HTTP là nền tảng giao tiếp dữ liệu cho World Wide Web
HTTP NHẬN
Phương thức HTTP GET yêu cầu biểu diễn tài nguyên đã chỉ định
NHẬN yêu cầu
- chỉ nên được sử dụng để yêu cầu một tài nguyên
- thông số được hiển thị trong URL
- có thể được lưu trữ
- vẫn còn trong lịch sử trình duyệt
- có thể được đánh dấu
- không bao giờ nên được sử dụng khi xử lý dữ liệu nhạy cảm
- có giới hạn chiều dài
Phương thức HTTP POST gửi dữ liệu đến máy chủ. Nó thường được sử dụng khi tải lên một tệp hoặc khi gửi một biểu mẫu web đã hoàn thành
POST yêu cầu
- nên được sử dụng để tạo ra một tài nguyên
- thông số không được hiển thị trong URL
- không bao giờ được lưu trữ
- không lưu lại trong lịch sử trình duyệt
- không thể được đánh dấu
- có thể được sử dụng khi xử lý dữ liệu nhạy cảm
- không có giới hạn chiều dài
PHP $_GET và $_POST
PHP cung cấp các siêu toàn cầu
The example retrieves the
name
andmessage
parameters from the$_POST
variable.$ php -S localhost:8000 post_req.php9 và$ curl -d "name=Lucia&message=Cau" localhost:8000 Lucia says: Cau0.The example retrieves the
name
andmessage
parameters from the$_POST
variable.$ php -S localhost:8000 post_req.php9 là một mảng kết hợp gồm các biến được chuyển đến tập lệnh hiện tại thông qua các tham số URL [chuỗi truy vấn].$ curl -d "name=Lucia&message=Cau" localhost:8000 Lucia says: Cau0 là một mảng biến kết hợp được chuyển đến tập lệnh hiện tại thông qua phương thức HTTP POST khi sử dụng$ curl -d "name=Lucia&message=Cau" localhost:8000 Lucia says: Cau3 hoặc$ curl -d "name=Lucia&message=Cau" localhost:8000 Lucia says: Cau4 làm Loại nội dung HTTP trong yêu cầuTrong ví dụ sau, chúng tôi tạo một yêu cầu GET bằng công cụ curl và xử lý yêu cầu bằng PHP thuần túy
The example retrieves the
Advertisementsname
andmessage
parameters from the$_GET
variable.$ php -S localhost:8000 get_req.phpChúng tôi bắt đầu máy chủ
________số 8Chúng tôi gửi hai yêu cầu GET với curl
PHP POST yêu cầu
Trong ví dụ sau, chúng tôi tạo một yêu cầu POST bằng công cụ curl và xử lý yêu cầu bằng PHP thuần túy
The example retrieves the
name
andmessage
parameters from the$_POST
variable.$ php -S localhost:8000 post_req.phpChúng tôi bắt đầu máy chủ
Quảng cáo$ curl -d "name=Lucia&message=Cau" localhost:8000 Lucia says: CauChúng tôi gửi một yêu cầu POST với curl
PHP gửi yêu cầu GET với Symfony HttpClient
Symfony cung cấp thành phần
$ curl -d "name=Lucia&message=Cau" localhost:8000 Lucia says: Cau5 cho phép chúng tôi tạo các yêu cầu HTTP trong PHPThe example retrieves the
name
andmessage
parameters from the$_POST
variable.$ php -S localhost:8000 post_req.php2Chúng tôi cài đặt thành phần
$ curl -d "name=Lucia&message=Cau" localhost:8000 Lucia says: Cau6The example retrieves the
name
andmessage
parameters from the$_POST
variable.$ php -S localhost:8000 post_req.php4Ví dụ gửi một yêu cầu GET với hai tham số truy vấn tới
$ curl -d "name=Lucia&message=Cau" localhost:8000 Lucia says: Cau7The example retrieves the
name
andmessage
parameters from the$_POST
variable.$ php -S localhost:8000 post_req.php6Chúng tôi bắt đầu máy chủ
The example retrieves the
name
andmessage
parameters from the$_POST
variable.$ php -S localhost:8000 post_req.php7Chúng tôi chạy tập lệnh
$ curl -d "name=Lucia&message=Cau" localhost:8000 Lucia says: Cau8PHP gửi yêu cầu POST với Symfony HttpClient
Trong ví dụ sau, chúng tôi gửi yêu cầu POST với Symfony HttpClient
The example retrieves the
name
andmessage
parameters from the$_POST
variable.$ php -S localhost:8000 post_req.php9Ví dụ gửi yêu cầu POST với hai tham số tới
$ curl -d "name=Lucia&message=Cau" localhost:8000 Lucia says: Cau9Quảng cáoThe example retrieves the
Advertisementsname
andmessage
parameters from the$_GET
variable.$ php -S localhost:8000 get_req.php0Chúng tôi bắt đầu máy chủ
The example retrieves the
Advertisementsname
andmessage
parameters from the$_GET
variable.$ php -S localhost:8000 get_req.php1Chúng tôi chạy tập lệnh
The example retrieves the
name
andmessage
parameters from the$_POST
variable.$ php -S localhost:8000 post_req.php20PHP NHẬN yêu cầu trong Symfony
Trong ví dụ sau, chúng tôi xử lý yêu cầu GET trong ứng dụng Symfony
The example retrieves the
Advertisementsname
andmessage
parameters from the$_GET
variable.$ php -S localhost:8000 get_req.php2Một ứng dụng mới được tạo
The example retrieves the
Advertisementsname
andmessage
parameters from the$_GET
variable.$ php -S localhost:8000 get_req.php3Chúng tôi cài đặt các thành phần
The example retrieves the
name
andmessage
parameters from the$_POST
variable.$ php -S localhost:8000 post_req.php21 vàThe example retrieves the
name
andmessage
parameters from the$_POST
variable.$ php -S localhost:8000 post_req.php22The example retrieves the
Advertisementsname
andmessage
parameters from the$_GET
variable.$ php -S localhost:8000 get_req.php4Chúng tôi tạo một bộ điều khiển mới
src/Trình điều khiển/Trình điều khiển gia đình. php
The example retrieves the
Advertisementsname
andmessage
parameters from the$_GET
variable.$ php -S localhost:8000 get_req.php5Bên trong phương thức
The example retrieves the
name
andmessage
parameters from the$_POST
variable.$ php -S localhost:8000 post_req.php23The example retrieves the
name
andmessage
parameters from the$_POST
variable.$ php -S localhost:8000 post_req.php24, chúng tôi nhận các tham số truy vấn và tạo phản hồiThe example retrieves the
name
andmessage
parameters from the$_GET
variable.Advertisements $ php -S localhost:8000 get_req.php6Tham số GET được truy xuất với
The example retrieves the
name
andmessage
parameters from the$_POST
variable.$ php -S localhost:8000 post_req.php25. Tham số thứ hai của phương thức là một giá trị mặc định được sử dụng khi không cóThe example retrieves the
Advertisementsname
andmessage
parameters from the$_GET
variable.$ php -S localhost:8000 get_req.php7Chúng tôi bắt đầu máy chủ
The example retrieves the
Advertisementsname
andmessage
parameters from the$_GET
variable.$ php -S localhost:8000 get_req.php8Chúng tôi tạo một yêu cầu GET với curl
Yêu cầu POST PHP trong Symfony
Trong ví dụ sau, chúng tôi xử lý yêu cầu POST trong ứng dụng Symfony
src/Trình điều khiển/Trình điều khiển gia đình. php
The example retrieves the
Advertisementsname
andmessage
parameters from the$_GET
variable.$ php -S localhost:8000 get_req.php9Chúng tôi thay đổi bộ điều khiển để xử lý yêu cầu POST
Quảng cáo$ curl 'localhost:8000/?name=Lucia&message=Cau' Lucia says: Cau $ curl 'localhost:8000/?name=Lucia' Lucia says: hello there0Tham số POST được truy xuất bằng
The example retrieves the
name
andmessage
parameters from the$_POST
variable.$ php -S localhost:8000 post_req.php26. Tham số thứ hai của phương thức là một giá trị mặc định được sử dụng khi không cóThe example retrieves the
Advertisementsname
andmessage
parameters from the$_GET
variable.$ php -S localhost:8000 get_req.php7Chúng tôi bắt đầu máy chủ
$ curl 'localhost:8000/?name=Lucia&message=Cau' Lucia says: Cau $ curl 'localhost:8000/?name=Lucia' Lucia says: hello there2Chúng tôi tạo một yêu cầu POST với curl
PHP NHẬN yêu cầu trong Slim
Trong ví dụ sau, chúng tôi sẽ xử lý yêu cầu GET trong khung Slim
$ curl 'localhost:8000/?name=Lucia&message=Cau' Lucia says: Cau $ curl 'localhost:8000/?name=Lucia' Lucia says: hello there3Chúng tôi cài đặt các gói
The example retrieves the
name
andmessage
parameters from the$_POST
variable.$ php -S localhost:8000 post_req.php27,The example retrieves the
name
andmessage
parameters from the$_POST
variable.$ php -S localhost:8000 post_req.php28 vàThe example retrieves the
name
andmessage
parameters from the$_POST
variable.$ php -S localhost:8000 post_req.php29$ curl 'localhost:8000/?name=Lucia&message=Cau' Lucia says: Cau $ curl 'localhost:8000/?name=Lucia' Lucia says: hello there4Chúng tôi nhận các tham số và trả về phản hồi trong Slim
$ curl 'localhost:8000/?name=Lucia&message=Cau' Lucia says: Cau $ curl 'localhost:8000/?name=Lucia' Lucia says: hello there5Tham số truy vấn được truy xuất bằng
The example retrieves the
name
andmessage
parameters from the$_POST
variable.$ php -S localhost:8000 post_req.php40;$ curl 'localhost:8000/?name=Lucia&message=Cau' Lucia says: Cau $ curl 'localhost:8000/?name=Lucia' Lucia says: hello there6Chúng tôi viết đầu ra cho phần phản hồi với
The example retrieves the
name
andmessage
parameters from the$_POST
variable.$ php -S localhost:8000 post_req.php41 $ curl 'localhost:8000/?name=Lucia&message=Cau' Lucia says: Cau $ curl 'localhost:8000/?name=Lucia' Lucia says: hello there7Chúng tôi bắt đầu máy chủ
The example retrieves the
Advertisementsname
andmessage
parameters from the$_GET
variable.$ php -S localhost:8000 get_req.php8Chúng tôi tạo một yêu cầu GET với curl
Trong ví dụ sau, chúng tôi sẽ xử lý một yêu cầu POST trong khung Slim
$ curl 'localhost:8000/?name=Lucia&message=Cau' Lucia says: Cau $ curl 'localhost:8000/?name=Lucia' Lucia says: hello there9Chúng tôi nhận các tham số POST và trả về phản hồi trong Slim
The example retrieves the
name
andmessage
parameters from the$_POST
variable.$ php -S localhost:8000 post_req.php0Các tham số POST được truy xuất bằng
The example retrieves the
name
andmessage
parameters from the$_POST
variable.$ php -S localhost:8000 post_req.php42$ curl 'localhost:8000/?name=Lucia&message=Cau' Lucia says: Cau $ curl 'localhost:8000/?name=Lucia' Lucia says: hello there7Chúng tôi bắt đầu máy chủ
$ curl 'localhost:8000/?name=Lucia&message=Cau' Lucia says: Cau $ curl 'localhost:8000/?name=Lucia' Lucia says: hello there2Chúng tôi tạo một yêu cầu POST với curl
PHP NHẬN yêu cầu trong Laravel
Trong ví dụ sau, chúng tôi xử lý một yêu cầu GET trong Laravel
The example retrieves the
name
andmessage
parameters from the$_POST
variable.$ php -S localhost:8000 post_req.php3Chúng tôi tạo một ứng dụng Laravel mới
The example retrieves the
name
andmessage
parameters from the$_POST
variable.$ php -S localhost:8000 post_req.php4Chúng tôi nhận các tham số GET và tạo phản hồi
Quảng cáoThe example retrieves the
name
andmessage
parameters from the$_POST
variable.$ php -S localhost:8000 post_req.php5Chúng tôi bắt đầu máy chủ
The example retrieves the
name
andmessage
parameters from the$_POST
variable.$ php -S localhost:8000 post_req.php6Chúng tôi gửi một yêu cầu NHẬN với curl
quảng cáoTrong ví dụ sau, chúng tôi gửi yêu cầu POST từ biểu mẫu HTML
tài nguyên/lượt xem/trang chủ. lưỡi. php
The example retrieves the
name
andmessage
parameters from the$_POST
variable.$ php -S localhost:8000 post_req.php7Chúng tôi có biểu mẫu POST trong mẫu Blade. Laravel yêu cầu bảo vệ CSRF cho các yêu cầu POST. Chúng tôi kích hoạt bảo vệ CSRF với
The example retrieves the
name
andmessage
parameters from the$_POST
variable.$ php -S localhost:8000 post_req.php43The example retrieves the
name
andmessage
parameters from the$_POST
variable.$ php -S localhost:8000 post_req.php8Chúng tôi xác thực và truy xuất các tham số POST và gửi chúng trong phản hồi. Ví dụ này nên được thử nghiệm trong một trình duyệt
Trong hướng dẫn này, chúng ta đã làm việc với các yêu cầu GET và POST trong PHP, Symfony, Slim và Laravel đơn giản
Yêu cầu HTTP trong PHP là gì?
Giao thức truyền tải siêu văn bản [HTTP] được thiết kế để cho phép liên lạc giữa máy khách và máy chủ. HTTP hoạt động như giao thức yêu cầu-phản hồi giữa máy khách và máy chủ . Ví dụ. Máy khách [trình duyệt] gửi yêu cầu HTTP đến máy chủ; .Làm cách nào để gửi yêu cầu HTTP trong PHP?
5 cách tạo yêu cầu HTTP trong PHP .Trình bao bọc luồng HTTP/s của PHPPhần mở rộng cURL của PHPGuzzleHttpHttpfulMáy khách HTTP của SymfonyTại sao lại sử dụng yêu cầu $_ trong PHP?
$_REQUEST của PHP được sử dụng rộng rãi để thu thập thông tin sau khi gửi từ biểu mẫu duyệt HTML . Hàm $_REQUEST được sử dụng để lấy thông tin biểu mẫu được gửi bằng phương thức POST của nó và phương thức GET khác.Làm cách nào để sử dụng API HTTP trong PHP?
Tạo tệp API REST. Tạo chỉ mục hoặc tệp HTML. .Nhận khóa API. Khi bạn bắt đầu với API, bạn nên đăng ký và nhận một khóa là một chuỗi các chữ cái và số. .Kiểm tra API với các ứng dụng PHP. Quá trình này kiểm tra xem mọi thứ có hoạt động như mong đợi khôngPhát triển ứng dụng PHP cần thiết bằng cách sử dụng API