Yêu cầu HTTP PHP

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 and message parameters from the $_POST variable.

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

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

$ php -S localhost:8000 post_req.php
9 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: Cau
0 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: Cau
3 hoặc
$ curl -d "name=Lucia&message=Cau" localhost:8000
Lucia says: Cau
4 làm Loại nội dung HTTP trong yêu cầu

Trong 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 name and message parameters from the $_GET variable.

Advertisements
$ php -S localhost:8000 get_req.php

Chúng tôi bắt đầu máy chủ

________số 8

Chú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 and message parameters from the $_POST variable.

$ php -S localhost:8000 post_req.php

Chúng tôi bắt đầu máy chủ

Quảng cáo
$ curl -d "name=Lucia&message=Cau" localhost:8000
Lucia says: Cau

Chú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: Cau
5 cho phép chúng tôi tạo các yêu cầu HTTP trong PHP

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

$ php -S localhost:8000 post_req.php
2

Chúng tôi cài đặt thành phần

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

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

$ php -S localhost:8000 post_req.php
4

Ví 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: Cau
7

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

$ php -S localhost:8000 post_req.php
6

Chúng tôi bắt đầu máy chủ

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

$ php -S localhost:8000 post_req.php
7

Chúng tôi chạy tập lệnh

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

PHP 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 and message parameters from the $_POST variable.

$ php -S localhost:8000 post_req.php
9

Ví 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: Cau
9

Quảng cáo

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

Advertisements
$ php -S localhost:8000 get_req.php
0

Chúng tôi bắt đầu máy chủ

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

Advertisements
$ php -S localhost:8000 get_req.php
1

Chúng tôi chạy tập lệnh

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

$ php -S localhost:8000 post_req.php
20

PHP 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 name and message parameters from the $_GET variable.

Advertisements
$ php -S localhost:8000 get_req.php
2

Một ứng dụng mới được tạo

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

Advertisements
$ php -S localhost:8000 get_req.php
3

Chúng tôi cài đặt các thành phần

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

$ php -S localhost:8000 post_req.php
21 và

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

$ php -S localhost:8000 post_req.php
22

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

Advertisements
$ php -S localhost:8000 get_req.php
4

Chú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 name and message parameters from the $_GET variable.

Advertisements
$ php -S localhost:8000 get_req.php
5

Bên trong phương thức

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

$ php -S localhost:8000 post_req.php
23

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

$ php -S localhost:8000 post_req.php
24, chúng tôi nhận các tham số truy vấn và tạo phản hồi

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

Advertisements
$ php -S localhost:8000 get_req.php
6

Tham số GET được truy xuất với

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

$ php -S localhost:8000 post_req.php
25. 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 name and message parameters from the $_GET variable.

Advertisements
$ php -S localhost:8000 get_req.php
7

Chúng tôi bắt đầu máy chủ

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

Advertisements
$ php -S localhost:8000 get_req.php
8

Chú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 name and message parameters from the $_GET variable.

Advertisements
$ php -S localhost:8000 get_req.php
9

Chú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 there
0

Tham số POST được truy xuất bằng

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

$ php -S localhost:8000 post_req.php
26. 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 name and message parameters from the $_GET variable.

Advertisements
$ php -S localhost:8000 get_req.php
7

Chú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 there
2

Chú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 there
3

Chúng tôi cài đặt các gói

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

$ php -S localhost:8000 post_req.php
27,

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

$ php -S localhost:8000 post_req.php
28 và

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

$ php -S localhost:8000 post_req.php
29

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

Chú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 there
5

Tham số truy vấn được truy xuất bằng

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

$ php -S localhost:8000 post_req.php
40;

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

Chúng tôi viết đầu ra cho phần phản hồi với

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

$ php -S localhost:8000 post_req.php
41

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

Chúng tôi bắt đầu máy chủ

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

Advertisements
$ php -S localhost:8000 get_req.php
8

Chú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 there
9

Chúng tôi nhận các tham số POST và trả về phản hồi trong Slim

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

$ php -S localhost:8000 post_req.php
0

Các tham số POST được truy xuất bằng

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

$ php -S localhost:8000 post_req.php
42

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

Chú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 there
2

Chú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 and message parameters from the $_POST variable.

$ php -S localhost:8000 post_req.php
3

Chúng tôi tạo một ứng dụng Laravel mới

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

$ php -S localhost:8000 post_req.php
4

Chúng tôi nhận các tham số GET và tạo phản hồi

Quảng cáo

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

$ php -S localhost:8000 post_req.php
5

Chúng tôi bắt đầu máy chủ

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

$ php -S localhost:8000 post_req.php
6

Chúng tôi gửi một yêu cầu NHẬN với curl

quảng cáo

Trong 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 and message parameters from the $_POST variable.

$ php -S localhost:8000 post_req.php
7

Chú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 and message parameters from the $_POST variable.

$ php -S localhost:8000 post_req.php
43

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

$ php -S localhost:8000 post_req.php
8

Chú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 PHP
Phần mở rộng cURL của PHP
GuzzleHttp
Httpful
Máy khách HTTP của Symfony

Tạ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ông
Phát triển ứng dụng PHP cần thiết bằng cách sử dụng API

Chủ Đề