Bạn có thể tùy chỉnh các yêu cầu do khách hàng tạo và chuyển bằng các tùy chọn yêu cầu. Tùy chọn yêu cầu kiểm soát các khía cạnh khác nhau của yêu cầu bao gồm, tiêu đề, tham số chuỗi truy vấn, cài đặt thời gian chờ, phần thân của yêu cầu, v.v.
Tất cả các ví dụ sau đều sử dụng ứng dụng khách sau
$client = new GuzzleHttp\Client[['base_uri' => '//httpbin.org']];
allow_redirects
Tóm lượcMô tả hành vi chuyển hướng của một yêu cầu
các loại- bool
- mảng
[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]Hằng số
use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\UriInterface; $onRedirect = function[ RequestInterface $request, ResponseInterface $response, UriInterface $uri ] { echo 'Redirecting! ' . $request->getUri[] . ' to ' . $uri . "\n"; }; $res = $client->request['GET', '/redirect/3', [ 'allow_redirects' => [ 'max' => 10, // allow at most 10 redirects. 'strict' => true, // use "strict" RFC compliant redirects. 'referer' => true, // add a Referer header 'protocols' => ['https'], // only allow https URLs 'on_redirect' => $onRedirect, 'track_redirects' => true ] ]]; echo $res->getStatusCode[]; // 200 echo $res->getHeaderLine['X-Guzzle-Redirect-History']; // //first-redirect, //second-redirect, etc... echo $res->getHeaderLine['X-Guzzle-Redirect-Status-History']; // 301, 302, etc...2
Đặt thành
use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\UriInterface; $onRedirect = function[ RequestInterface $request, ResponseInterface $response, UriInterface $uri ] { echo 'Redirecting! ' . $request->getUri[] . ' to ' . $uri . "\n"; }; $res = $client->request['GET', '/redirect/3', [ 'allow_redirects' => [ 'max' => 10, // allow at most 10 redirects. 'strict' => true, // use "strict" RFC compliant redirects. 'referer' => true, // add a Referer header 'protocols' => ['https'], // only allow https URLs 'on_redirect' => $onRedirect, 'track_redirects' => true ] ]]; echo $res->getStatusCode[]; // 200 echo $res->getHeaderLine['X-Guzzle-Redirect-History']; // //first-redirect, //second-redirect, etc... echo $res->getHeaderLine['X-Guzzle-Redirect-Status-History']; // 301, 302, etc...3 để tắt chuyển hướng
$res = $client->request['GET', '/redirect/3', ['allow_redirects' => false]]; echo $res->getStatusCode[]; // 302
Đặt thành
use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\UriInterface; $onRedirect = function[ RequestInterface $request, ResponseInterface $response, UriInterface $uri ] { echo 'Redirecting! ' . $request->getUri[] . ' to ' . $uri . "\n"; }; $res = $client->request['GET', '/redirect/3', [ 'allow_redirects' => [ 'max' => 10, // allow at most 10 redirects. 'strict' => true, // use "strict" RFC compliant redirects. 'referer' => true, // add a Referer header 'protocols' => ['https'], // only allow https URLs 'on_redirect' => $onRedirect, 'track_redirects' => true ] ]]; echo $res->getStatusCode[]; // 200 echo $res->getHeaderLine['X-Guzzle-Redirect-History']; // //first-redirect, //second-redirect, etc... echo $res->getHeaderLine['X-Guzzle-Redirect-Status-History']; // 301, 302, etc...4 [cài đặt mặc định] để bật chuyển hướng bình thường với số lần chuyển hướng tối đa là 5
$res = $client->request['GET', '/redirect/3']; echo $res->getStatusCode[]; // 200
Bạn cũng có thể chuyển một mảng kết hợp chứa các cặp giá trị khóa sau
tối đa. [int, default=5] số lần chuyển hướng tối đa được phép
nghiêm khắc. [bool, default=false] Đặt thành true để sử dụng chuyển hướng nghiêm ngặt. Chuyển hướng tuân thủ RFC nghiêm ngặt có nghĩa là yêu cầu chuyển hướng POST được gửi dưới dạng yêu cầu POST so với. làm những gì mà hầu hết các trình duyệt làm đó là chuyển hướng các yêu cầu POST bằng các yêu cầu GET
người giới thiệu. [bool, default=false] Đặt thành true để cho phép thêm tiêu đề Người giới thiệu khi chuyển hướng
giao thức. [mảng, mặc định=['http', 'https']] Đã chỉ định giao thức nào được phép cho các yêu cầu chuyển hướng
on_redirect. [có thể gọi] PHP có thể gọi được gọi khi gặp chuyển hướng. Có thể gọi được gọi với yêu cầu ban đầu và phản hồi chuyển hướng đã nhận được. Mọi giá trị trả về từ hàm on_redirect đều bị bỏ qua
track_redirects. [bool] Khi được đặt thành
use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\UriInterface; $onRedirect = function[ RequestInterface $request, ResponseInterface $response, UriInterface $uri ] { echo 'Redirecting! ' . $request->getUri[] . ' to ' . $uri . "\n"; }; $res = $client->request['GET', '/redirect/3', [ 'allow_redirects' => [ 'max' => 10, // allow at most 10 redirects. 'strict' => true, // use "strict" RFC compliant redirects. 'referer' => true, // add a Referer header 'protocols' => ['https'], // only allow https URLs 'on_redirect' => $onRedirect, 'track_redirects' => true ] ]]; echo $res->getStatusCode[]; // 200 echo $res->getHeaderLine['X-Guzzle-Redirect-History']; // //first-redirect, //second-redirect, etc... echo $res->getHeaderLine['X-Guzzle-Redirect-Status-History']; // 301, 302, etc...
4, mỗi URI được chuyển hướng và mã trạng thái gặp phải sẽ được theo dõi trong các tiêu đềuse Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\UriInterface; $onRedirect = function[ RequestInterface $request, ResponseInterface $response, UriInterface $uri ] { echo 'Redirecting! ' . $request->getUri[] . ' to ' . $uri . "\n"; }; $res = $client->request['GET', '/redirect/3', [ 'allow_redirects' => [ 'max' => 10, // allow at most 10 redirects. 'strict' => true, // use "strict" RFC compliant redirects. 'referer' => true, // add a Referer header 'protocols' => ['https'], // only allow https URLs 'on_redirect' => $onRedirect, 'track_redirects' => true ] ]]; echo $res->getStatusCode[]; // 200 echo $res->getHeaderLine['X-Guzzle-Redirect-History']; // //first-redirect, //second-redirect, etc... echo $res->getHeaderLine['X-Guzzle-Redirect-Status-History']; // 301, 302, etc...
6 vàuse Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\UriInterface; $onRedirect = function[ RequestInterface $request, ResponseInterface $response, UriInterface $uri ] { echo 'Redirecting! ' . $request->getUri[] . ' to ' . $uri . "\n"; }; $res = $client->request['GET', '/redirect/3', [ 'allow_redirects' => [ 'max' => 10, // allow at most 10 redirects. 'strict' => true, // use "strict" RFC compliant redirects. 'referer' => true, // add a Referer header 'protocols' => ['https'], // only allow https URLs 'on_redirect' => $onRedirect, 'track_redirects' => true ] ]]; echo $res->getStatusCode[]; // 200 echo $res->getHeaderLine['X-Guzzle-Redirect-History']; // //first-redirect, //second-redirect, etc... echo $res->getHeaderLine['X-Guzzle-Redirect-Status-History']; // 301, 302, etc...
7 tương ứng. Tất cả các URI và mã trạng thái sẽ được lưu trữ theo thứ tự chuyển hướng gặp phảiGhi chú. Khi chuyển hướng theo dõi, tiêu đề
use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\UriInterface; $onRedirect = function[ RequestInterface $request, ResponseInterface $response, UriInterface $uri ] { echo 'Redirecting! ' . $request->getUri[] . ' to ' . $uri . "\n"; }; $res = $client->request['GET', '/redirect/3', [ 'allow_redirects' => [ 'max' => 10, // allow at most 10 redirects. 'strict' => true, // use "strict" RFC compliant redirects. 'referer' => true, // add a Referer header 'protocols' => ['https'], // only allow https URLs 'on_redirect' => $onRedirect, 'track_redirects' => true ] ]]; echo $res->getStatusCode[]; // 200 echo $res->getHeaderLine['X-Guzzle-Redirect-History']; // //first-redirect, //second-redirect, etc... echo $res->getHeaderLine['X-Guzzle-Redirect-Status-History']; // 301, 302, etc...
6 sẽ loại trừ URI của yêu cầu ban đầu và tiêu đềuse Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\UriInterface; $onRedirect = function[ RequestInterface $request, ResponseInterface $response, UriInterface $uri ] { echo 'Redirecting! ' . $request->getUri[] . ' to ' . $uri . "\n"; }; $res = $client->request['GET', '/redirect/3', [ 'allow_redirects' => [ 'max' => 10, // allow at most 10 redirects. 'strict' => true, // use "strict" RFC compliant redirects. 'referer' => true, // add a Referer header 'protocols' => ['https'], // only allow https URLs 'on_redirect' => $onRedirect, 'track_redirects' => true ] ]]; echo $res->getStatusCode[]; // 200 echo $res->getHeaderLine['X-Guzzle-Redirect-History']; // //first-redirect, //second-redirect, etc... echo $res->getHeaderLine['X-Guzzle-Redirect-Status-History']; // 301, 302, etc...
7 sẽ loại trừ mã trạng thái cuối cùng
use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\UriInterface; $onRedirect = function[ RequestInterface $request, ResponseInterface $response, UriInterface $uri ] { echo 'Redirecting! ' . $request->getUri[] . ' to ' . $uri . "\n"; }; $res = $client->request['GET', '/redirect/3', [ 'allow_redirects' => [ 'max' => 10, // allow at most 10 redirects. 'strict' => true, // use "strict" RFC compliant redirects. 'referer' => true, // add a Referer header 'protocols' => ['https'], // only allow https URLs 'on_redirect' => $onRedirect, 'track_redirects' => true ] ]]; echo $res->getStatusCode[]; // 200 echo $res->getHeaderLine['X-Guzzle-Redirect-History']; // //first-redirect, //second-redirect, etc... echo $res->getHeaderLine['X-Guzzle-Redirect-Status-History']; // 301, 302, etc...
Cảnh báo
Tùy chọn này chỉ có tác dụng nếu trình xử lý của bạn có phần mềm trung gian
$client->request['GET', '/get', ['auth' => ['username', 'password']]];0. Phần mềm trung gian này được thêm theo mặc định khi ứng dụng khách được tạo không có trình xử lý và được thêm theo mặc định khi tạo trình xử lý có ____13_______1
Ghi chú
Tùy chọn này không có hiệu lực khi đưa ra yêu cầu bằng cách sử dụng
$client->request['GET', '/get', ['auth' => ['username', 'password']]];2. Để luôn tuân thủ PSR-18, mọi phản hồi chuyển hướng đều được trả về như hiện tại
xác thực
Tóm lượcTruyền một mảng tham số xác thực HTTP để sử dụng với yêu cầu. Mảng phải chứa tên người dùng trong chỉ mục [0], mật khẩu trong chỉ mục [1] và bạn có thể tùy chọn cung cấp loại xác thực tích hợp trong chỉ mục [2]. Vượt qua
$client->request['GET', '/get', ['auth' => ['username', 'password']]];3 để tắt xác thực cho một yêu cầucác loại
- mảng
- chuỗi
- vô giá trị
Không có
Hằng số$client->request['GET', '/get', ['auth' => ['username', 'password']]];4
Các loại xác thực tích hợp như sau
basicSử dụng xác thực HTTP cơ bản trong tiêu đề$client->request['GET', '/get', ['auth' => ['username', 'password']]];5 [cài đặt mặc định được sử dụng nếu không có cài đặt nào được chỉ định]
$client->request['GET', '/get', ['auth' => ['username', 'password']]];digestSử dụng xác thực thông báo [phải được trình xử lý HTTP hỗ trợ]
$client->request['GET', '/get', [ 'auth' => ['username', 'password', 'digest'] ]];
Ghi chú
Điều này hiện chỉ được hỗ trợ khi sử dụng trình xử lý cURL, nhưng việc tạo một thay thế có thể được sử dụng với bất kỳ trình xử lý HTTP nào đã được lên kế hoạch
ntlmSử dụng xác thực NTLM của Microsoft [phải được trình xử lý HTTP hỗ trợ]$client->request['GET', '/get', [ 'auth' => ['username', 'password', 'ntlm'] ]];
Ghi chú
Điều này hiện chỉ được hỗ trợ khi sử dụng trình xử lý cURL
cơ thể người
Tóm lượcTùy chọn
$client->request['GET', '/get', ['auth' => ['username', 'password']]];6 được sử dụng để kiểm soát phần thân của thực thể kèm theo yêu cầu [e. g. , PUT, POST, VÁ]các loại
- chuỗi
- Tài nguyên
$client->request['GET', '/get', ['auth' => ['username', 'password']]];
7 $client->request['GET', '/get', ['auth' => ['username', 'password']]];
8
Không có
Hằng số$client->request['GET', '/get', ['auth' => ['username', 'password']]];9
Cài đặt này có thể được đặt thành bất kỳ loại nào sau đây
chuỗi
// You can send requests that use a string as the message body. $client->request['PUT', '/put', ['body' => 'foo']];
tài nguyên được trả về từ
$client->request['GET', '/get', ['auth' => ['username', 'password']]];
7// You can send requests that use a stream resource as the body. $resource = \GuzzleHttp\Psr7\Utils::tryFopen['//httpbin.org', 'r']; $client->request['PUT', '/put', ['body' => $resource]];
$client->request['GET', '/get', ['auth' => ['username', 'password']]];
8[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]
0
Ghi chú
Không thể sử dụng tùy chọn này với
$client->request['GET', '/get', [ 'auth' => ['username', 'password', 'digest'] ]];2,
$client->request['GET', '/get', [ 'auth' => ['username', 'password', 'digest'] ]];3 hoặc
$client->request['GET', '/get', [ 'auth' => ['username', 'password', 'digest'] ]];4
chứng chỉ
Tóm lượcĐặt thành một chuỗi để chỉ định đường dẫn đến tệp chứa chứng chỉ phía máy khách được định dạng PEM. Nếu mật khẩu được yêu cầu, thì hãy đặt thành một mảng chứa đường dẫn đến tệp PEM trong phần tử mảng đầu tiên, theo sau là mật khẩu cần thiết cho chứng chỉ trong phần tử mảng thứ hai
các loại- chuỗi
- mảng
Không có
Hằng số$client->request['GET', '/get', [ 'auth' => ['username', 'password', 'digest'] ]];5
[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]1
bánh quy
Tóm tắtChỉ định có sử dụng cookie trong một yêu cầu hay không hoặc sử dụng lọ cookie nào hoặc gửi cookie nào. Types$client->request['GET', '/get', [ 'auth' => ['username', 'password', 'digest'] ]];6DefaultNoneConstant
$client->request['GET', '/get', [ 'auth' => ['username', 'password', 'digest'] ]];7
Bạn phải chỉ định tùy chọn cookie là
$client->request['GET', '/get', [ 'auth' => ['username', 'password', 'digest'] ]];6 hoặc
use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\UriInterface; $onRedirect = function[ RequestInterface $request, ResponseInterface $response, UriInterface $uri ] { echo 'Redirecting! ' . $request->getUri[] . ' to ' . $uri . "\n"; }; $res = $client->request['GET', '/redirect/3', [ 'allow_redirects' => [ 'max' => 10, // allow at most 10 redirects. 'strict' => true, // use "strict" RFC compliant redirects. 'referer' => true, // add a Referer header 'protocols' => ['https'], // only allow https URLs 'on_redirect' => $onRedirect, 'track_redirects' => true ] ]]; echo $res->getStatusCode[]; // 200 echo $res->getHeaderLine['X-Guzzle-Redirect-History']; // //first-redirect, //second-redirect, etc... echo $res->getHeaderLine['X-Guzzle-Redirect-Status-History']; // 301, 302, etc...3
[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]2
Cảnh báo
Tùy chọn này chỉ có tác dụng nếu trình xử lý của bạn có phần mềm trung gian
$client->request['GET', '/get', [ 'auth' => ['username', 'password', 'ntlm'] ]];0. Phần mềm trung gian này được thêm theo mặc định khi ứng dụng khách được tạo không có trình xử lý và được thêm theo mặc định khi tạo trình xử lý có
$client->request['GET', '/get', [ 'auth' => ['username', 'password', 'ntlm'] ]];1
Mẹo
Khi tạo ứng dụng khách, bạn có thể đặt tùy chọn cookie mặc định thành
use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\UriInterface; $onRedirect = function[ RequestInterface $request, ResponseInterface $response, UriInterface $uri ] { echo 'Redirecting! ' . $request->getUri[] . ' to ' . $uri . "\n"; }; $res = $client->request['GET', '/redirect/3', [ 'allow_redirects' => [ 'max' => 10, // allow at most 10 redirects. 'strict' => true, // use "strict" RFC compliant redirects. 'referer' => true, // add a Referer header 'protocols' => ['https'], // only allow https URLs 'on_redirect' => $onRedirect, 'track_redirects' => true ] ]]; echo $res->getStatusCode[]; // 200 echo $res->getHeaderLine['X-Guzzle-Redirect-History']; // //first-redirect, //second-redirect, etc... echo $res->getHeaderLine['X-Guzzle-Redirect-Status-History']; // 301, 302, etc...4 để sử dụng phiên cookie được chia sẻ được liên kết với ứng dụng khách
connect_timeout
SummaryFloat mô tả số giây phải đợi trong khi cố gắng kết nối với máy chủ. Sử dụng$client->request['GET', '/get', [ 'auth' => ['username', 'password', 'ntlm'] ]];3 để đợi vô thời hạn [hành vi mặc định]. TypesfloatDefault
$client->request['GET', '/get', [ 'auth' => ['username', 'password', 'ntlm'] ]];3Constant
$client->request['GET', '/get', [ 'auth' => ['username', 'password', 'ntlm'] ]];5
[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]3
Ghi chú
Cài đặt này phải được hỗ trợ bởi trình xử lý HTTP được sử dụng để gửi yêu cầu.
$client->request['GET', '/get', [ 'auth' => ['username', 'password', 'ntlm'] ]];6 hiện chỉ được hỗ trợ bởi trình xử lý cURL tích hợp
gỡ lỗi
Tóm lượcĐặt thành
use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\UriInterface; $onRedirect = function[ RequestInterface $request, ResponseInterface $response, UriInterface $uri ] { echo 'Redirecting! ' . $request->getUri[] . ' to ' . $uri . "\n"; }; $res = $client->request['GET', '/redirect/3', [ 'allow_redirects' => [ 'max' => 10, // allow at most 10 redirects. 'strict' => true, // use "strict" RFC compliant redirects. 'referer' => true, // add a Referer header 'protocols' => ['https'], // only allow https URLs 'on_redirect' => $onRedirect, 'track_redirects' => true ] ]]; echo $res->getStatusCode[]; // 200 echo $res->getHeaderLine['X-Guzzle-Redirect-History']; // //first-redirect, //second-redirect, etc... echo $res->getHeaderLine['X-Guzzle-Redirect-Status-History']; // 301, 302, etc...4 hoặc đặt thành luồng PHP được trả về bởi
$client->request['GET', '/get', ['auth' => ['username', 'password']]];7 để bật đầu ra gỡ lỗi với trình xử lý được sử dụng để gửi yêu cầu. Ví dụ: khi sử dụng cURL để chuyển yêu cầu, đoạn dài của cURL là
$client->request['GET', '/get', [ 'auth' => ['username', 'password', 'ntlm'] ]];9 sẽ được phát ra. Khi sử dụng trình bao bọc luồng PHP, các thông báo về trình bao bọc luồng sẽ được phát ra. Nếu được đặt thành true, đầu ra được ghi vào STDOUT của PHP. Nếu một luồng PHP được cung cấp, đầu ra được ghi vào luồngcác loại
- bool
- Tài nguyên
$client->request['GET', '/get', ['auth' => ['username', 'password']]];
7
Không có
Hằng số// You can send requests that use a string as the message body. $client->request['PUT', '/put', ['body' => 'foo']];1
[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]4
Chạy ví dụ trên sẽ xuất ra kết quả như sau
[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]5
giải mã_nội dung
Tóm lượcChỉ định có hay không
// You can send requests that use a string as the message body. $client->request['PUT', '/put', ['body' => 'foo']];2 phản hồi [gzip, deflate, v.v. ] được tự động giải mãcác loại
- chuỗi
- bool
use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\UriInterface; $onRedirect = function[ RequestInterface $request, ResponseInterface $response, UriInterface $uri ] { echo 'Redirecting! ' . $request->getUri[] . ' to ' . $uri . "\n"; }; $res = $client->request['GET', '/redirect/3', [ 'allow_redirects' => [ 'max' => 10, // allow at most 10 redirects. 'strict' => true, // use "strict" RFC compliant redirects. 'referer' => true, // add a Referer header 'protocols' => ['https'], // only allow https URLs 'on_redirect' => $onRedirect, 'track_redirects' => true ] ]]; echo $res->getStatusCode[]; // 200 echo $res->getHeaderLine['X-Guzzle-Redirect-History']; // //first-redirect, //second-redirect, etc... echo $res->getHeaderLine['X-Guzzle-Redirect-Status-History']; // 301, 302, etc...4Hằng số
// You can send requests that use a string as the message body. $client->request['PUT', '/put', ['body' => 'foo']];4
Tùy chọn này có thể được sử dụng để kiểm soát cách xử lý nội dung phản hồi được mã hóa nội dung. Theo mặc định,
// You can send requests that use a string as the message body. $client->request['PUT', '/put', ['body' => 'foo']];5 được đặt thành true, nghĩa là mọi phản hồi được nén hoặc xì hơi sẽ được giải mã bởi Guzzle
Khi được đặt thành
use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\UriInterface; $onRedirect = function[ RequestInterface $request, ResponseInterface $response, UriInterface $uri ] { echo 'Redirecting! ' . $request->getUri[] . ' to ' . $uri . "\n"; }; $res = $client->request['GET', '/redirect/3', [ 'allow_redirects' => [ 'max' => 10, // allow at most 10 redirects. 'strict' => true, // use "strict" RFC compliant redirects. 'referer' => true, // add a Referer header 'protocols' => ['https'], // only allow https URLs 'on_redirect' => $onRedirect, 'track_redirects' => true ] ]]; echo $res->getStatusCode[]; // 200 echo $res->getHeaderLine['X-Guzzle-Redirect-History']; // //first-redirect, //second-redirect, etc... echo $res->getHeaderLine['X-Guzzle-Redirect-Status-History']; // 301, 302, etc...3, nội dung của phản hồi không bao giờ được giải mã, nghĩa là các byte đi qua trình xử lý không thay đổi
[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]6
Khi được đặt thành chuỗi, các byte của phản hồi được giải mã và giá trị chuỗi được cung cấp cho tùy chọn
// You can send requests that use a string as the message body. $client->request['PUT', '/put', ['body' => 'foo']];5 được chuyển dưới dạng tiêu đề
// You can send requests that use a string as the message body. $client->request['PUT', '/put', ['body' => 'foo']];8 của yêu cầu
[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]7
sự chậm trễ
Tóm lượcSố mili giây để trì hoãn trước khi gửi yêu cầu
các loại- số nguyên
- trôi nổi
vô giá trị
Hằng số// You can send requests that use a string as the message body. $client->request['PUT', '/put', ['body' => 'foo']];9
mong đợi
Tóm lượcKiểm soát hành vi của "Expect. tiêu đề 100-Tiếp tục"
các loại- bool
- số nguyên
// You can send requests that use a stream resource as the body. $resource = \GuzzleHttp\Psr7\Utils::tryFopen['//httpbin.org', 'r']; $client->request['PUT', '/put', ['body' => $resource]];0Hằng số
// You can send requests that use a stream resource as the body. $resource = \GuzzleHttp\Psr7\Utils::tryFopen['//httpbin.org', 'r']; $client->request['PUT', '/put', ['body' => $resource]];1
Đặt thành
use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\UriInterface; $onRedirect = function[ RequestInterface $request, ResponseInterface $response, UriInterface $uri ] { echo 'Redirecting! ' . $request->getUri[] . ' to ' . $uri . "\n"; }; $res = $client->request['GET', '/redirect/3', [ 'allow_redirects' => [ 'max' => 10, // allow at most 10 redirects. 'strict' => true, // use "strict" RFC compliant redirects. 'referer' => true, // add a Referer header 'protocols' => ['https'], // only allow https URLs 'on_redirect' => $onRedirect, 'track_redirects' => true ] ]]; echo $res->getStatusCode[]; // 200 echo $res->getHeaderLine['X-Guzzle-Redirect-History']; // //first-redirect, //second-redirect, etc... echo $res->getHeaderLine['X-Guzzle-Redirect-Status-History']; // 301, 302, etc...4 để bật tùy chọn "Mong đợi. 100-Continue" cho tất cả các yêu cầu gửi nội dung. Đặt thành
use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\UriInterface; $onRedirect = function[ RequestInterface $request, ResponseInterface $response, UriInterface $uri ] { echo 'Redirecting! ' . $request->getUri[] . ' to ' . $uri . "\n"; }; $res = $client->request['GET', '/redirect/3', [ 'allow_redirects' => [ 'max' => 10, // allow at most 10 redirects. 'strict' => true, // use "strict" RFC compliant redirects. 'referer' => true, // add a Referer header 'protocols' => ['https'], // only allow https URLs 'on_redirect' => $onRedirect, 'track_redirects' => true ] ]]; echo $res->getStatusCode[]; // 200 echo $res->getHeaderLine['X-Guzzle-Redirect-History']; // //first-redirect, //second-redirect, etc... echo $res->getHeaderLine['X-Guzzle-Redirect-Status-History']; // 301, 302, etc...3 để tắt tùy chọn "Mong đợi. tiêu đề 100-Continue" cho tất cả các yêu cầu. Đặt thành một số sao cho kích thước của tải trọng phải lớn hơn số để gửi tiêu đề Kỳ vọng. Đặt thành một số sẽ gửi tiêu đề Kỳ vọng cho tất cả các yêu cầu trong đó kích thước của tải trọng không thể xác định được hoặc khi phần thân không thể tua lại
Theo mặc định, Guzzle sẽ thêm dòng "Expect. 100-Continue" khi kích thước nội dung của yêu cầu lớn hơn 1 MB và yêu cầu đang sử dụng HTTP/1. 1
Ghi chú
Tùy chọn này chỉ có hiệu lực khi sử dụng HTTP/1. 1. HTTP/1. 0 và HTTP/2. 0 không hỗ trợ tùy chọn "Mong đợi. tiêu đề 100-Tiếp tục". Hỗ trợ xử lý lỗi "Mong đợi. Quy trình làm việc 100-Continue" phải được triển khai bởi trình xử lý Guzzle HTTP được khách hàng sử dụng
force_ip_resolve
Tóm tắtĐặt thành "v4" nếu bạn muốn trình xử lý HTTP chỉ sử dụng giao thức ipv4 hoặc "v6" cho giao thức ipv6. TypestringDefaultnullConstant// You can send requests that use a stream resource as the body. $resource = \GuzzleHttp\Psr7\Utils::tryFopen['//httpbin.org', 'r']; $client->request['PUT', '/put', ['body' => $resource]];4
[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]8
Ghi chú
Cài đặt này phải được hỗ trợ bởi trình xử lý HTTP được sử dụng để gửi yêu cầu.
// You can send requests that use a stream resource as the body. $resource = \GuzzleHttp\Psr7\Utils::tryFopen['//httpbin.org', 'r']; $client->request['PUT', '/put', ['body' => $resource]];5 hiện chỉ được hỗ trợ bởi trình xử lý luồng và cURL tích hợp
form_params
Tóm tắtĐược sử dụng để gửi yêu cầu POST ứng dụng/x-www-form-urlencoded. TypearrayConstant// You can send requests that use a stream resource as the body. $resource = \GuzzleHttp\Psr7\Utils::tryFopen['//httpbin.org', 'r']; $client->request['PUT', '/put', ['body' => $resource]];6
Mảng liên kết của tên trường biểu mẫu với giá trị trong đó mỗi giá trị là một chuỗi hoặc mảng chuỗi. Đặt tiêu đề Loại nội dung thành ứng dụng/x-www-form-urlencoded khi chưa có tiêu đề Loại nội dung
[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]9
Ghi chú
Không thể sử dụng
$client->request['GET', '/get', [ 'auth' => ['username', 'password', 'digest'] ]];2 với tùy chọn
$client->request['GET', '/get', [ 'auth' => ['username', 'password', 'digest'] ]];3. Bạn sẽ cần phải sử dụng cái này hay cái kia. Sử dụng
$client->request['GET', '/get', [ 'auth' => ['username', 'password', 'digest'] ]];2 cho yêu cầu
[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]00 và
$client->request['GET', '/get', [ 'auth' => ['username', 'password', 'digest'] ]];3 cho yêu cầu
[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]02
Không thể sử dụng tùy chọn này với
$client->request['GET', '/get', ['auth' => ['username', 'password']]];6,
$client->request['GET', '/get', [ 'auth' => ['username', 'password', 'digest'] ]];3 hoặc
$client->request['GET', '/get', [ 'auth' => ['username', 'password', 'digest'] ]];4
tiêu đề
Mảng tiêu đề SummaryAssociative để thêm vào yêu cầu. Mỗi khóa là tên của tiêu đề và mỗi giá trị là một chuỗi hoặc mảng chuỗi đại diện cho các giá trị trường tiêu đề. TypearrayDefaultsNoneConstant_______1_______06$res = $client->request['GET', '/redirect/3', ['allow_redirects' => false]]; echo $res->getStatusCode[]; // 3020
Tiêu đề có thể được thêm làm tùy chọn mặc định khi tạo ứng dụng khách. Khi tiêu đề được sử dụng làm tùy chọn mặc định, chúng chỉ được áp dụng nếu yêu cầu được tạo chưa chứa tiêu đề cụ thể. Điều này bao gồm cả các yêu cầu được chuyển đến máy khách theo các phương thức
[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]07 và
[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]08 và các yêu cầu do máy khách tạo ra [e. g. ,
[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]09 và
[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]10]
$res = $client->request['GET', '/redirect/3', ['allow_redirects' => false]]; echo $res->getStatusCode[]; // 3021
http_errors
Tóm tắtĐặt thànhuse Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\UriInterface; $onRedirect = function[ RequestInterface $request, ResponseInterface $response, UriInterface $uri ] { echo 'Redirecting! ' . $request->getUri[] . ' to ' . $uri . "\n"; }; $res = $client->request['GET', '/redirect/3', [ 'allow_redirects' => [ 'max' => 10, // allow at most 10 redirects. 'strict' => true, // use "strict" RFC compliant redirects. 'referer' => true, // add a Referer header 'protocols' => ['https'], // only allow https URLs 'on_redirect' => $onRedirect, 'track_redirects' => true ] ]]; echo $res->getStatusCode[]; // 200 echo $res->getHeaderLine['X-Guzzle-Redirect-History']; // //first-redirect, //second-redirect, etc... echo $res->getHeaderLine['X-Guzzle-Redirect-Status-History']; // 301, 302, etc...3 để vô hiệu hóa các ngoại lệ đối với lỗi giao thức HTTP [i. e. , phản hồi 4xx và 5xx]. Các ngoại lệ được đưa ra theo mặc định khi gặp lỗi giao thức HTTP. TypesboolDefault
use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\UriInterface; $onRedirect = function[ RequestInterface $request, ResponseInterface $response, UriInterface $uri ] { echo 'Redirecting! ' . $request->getUri[] . ' to ' . $uri . "\n"; }; $res = $client->request['GET', '/redirect/3', [ 'allow_redirects' => [ 'max' => 10, // allow at most 10 redirects. 'strict' => true, // use "strict" RFC compliant redirects. 'referer' => true, // add a Referer header 'protocols' => ['https'], // only allow https URLs 'on_redirect' => $onRedirect, 'track_redirects' => true ] ]]; echo $res->getStatusCode[]; // 200 echo $res->getHeaderLine['X-Guzzle-Redirect-History']; // //first-redirect, //second-redirect, etc... echo $res->getHeaderLine['X-Guzzle-Redirect-Status-History']; // 301, 302, etc...4Constant
[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]13
$res = $client->request['GET', '/redirect/3', ['allow_redirects' => false]]; echo $res->getStatusCode[]; // 3022
Cảnh báo
Tùy chọn này chỉ có tác dụng nếu trình xử lý của bạn có phần mềm trung gian
[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]14. Phần mềm trung gian này được thêm theo mặc định khi ứng dụng khách được tạo không có trình xử lý và được thêm theo mặc định khi tạo trình xử lý có
$client->request['GET', '/get', [ 'auth' => ['username', 'password', 'ntlm'] ]];1
idn_conversion
Tóm lượcHỗ trợ tên miền quốc tế hóa [IDN] [được bật theo mặc định nếu có tiện ích mở rộng
[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]16]các loại
- bool
- int
use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\UriInterface; $onRedirect = function[ RequestInterface $request, ResponseInterface $response, UriInterface $uri ] { echo 'Redirecting! ' . $request->getUri[] . ' to ' . $uri . "\n"; }; $res = $client->request['GET', '/redirect/3', [ 'allow_redirects' => [ 'max' => 10, // allow at most 10 redirects. 'strict' => true, // use "strict" RFC compliant redirects. 'referer' => true, // add a Referer header 'protocols' => ['https'], // only allow https URLs 'on_redirect' => $onRedirect, 'track_redirects' => true ] ]]; echo $res->getStatusCode[]; // 200 echo $res->getHeaderLine['X-Guzzle-Redirect-History']; // //first-redirect, //second-redirect, etc... echo $res->getHeaderLine['X-Guzzle-Redirect-Status-History']; // 301, 302, etc...4 nếu có tiện ích mở rộng
[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]16 [và thư viện ICU là 4. 6+ cho PHP 7. 2+],
use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\UriInterface; $onRedirect = function[ RequestInterface $request, ResponseInterface $response, UriInterface $uri ] { echo 'Redirecting! ' . $request->getUri[] . ' to ' . $uri . "\n"; }; $res = $client->request['GET', '/redirect/3', [ 'allow_redirects' => [ 'max' => 10, // allow at most 10 redirects. 'strict' => true, // use "strict" RFC compliant redirects. 'referer' => true, // add a Referer header 'protocols' => ['https'], // only allow https URLs 'on_redirect' => $onRedirect, 'track_redirects' => true ] ]]; echo $res->getStatusCode[]; // 200 echo $res->getHeaderLine['X-Guzzle-Redirect-History']; // //first-redirect, //second-redirect, etc... echo $res->getHeaderLine['X-Guzzle-Redirect-Status-History']; // 301, 302, etc...3 ngược lạiHằng số
[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]20
$res = $client->request['GET', '/redirect/3', ['allow_redirects' => false]]; echo $res->getStatusCode[]; // 3023
Bật/tắt hỗ trợ IDN, cũng có thể được sử dụng để kiểm soát chính xác bằng cách kết hợp các hằng số IDNA_* [ngoại trừ IDNA_ERROR_*], xem tham số
[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]21 trong tài liệu idn_to_ascii[] để biết thêm chi tiết
json
Tóm tắt Tùy chọn$client->request['GET', '/get', [ 'auth' => ['username', 'password', 'digest'] ]];4 được sử dụng để dễ dàng tải lên dữ liệu được mã hóa JSON dưới dạng nội dung của yêu cầu. Tiêu đề Loại Nội dung của
[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]23 sẽ được thêm vào nếu chưa có tiêu đề Loại Nội dung nào trên thư. Các loạiBất kỳ loại PHP nào có thể được vận hành bởi hàm
[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]24 của PHP. Mặc địnhNoneConstant
[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]25
$res = $client->request['GET', '/redirect/3', ['allow_redirects' => false]]; echo $res->getStatusCode[]; // 3024
Đây là một ví dụ về việc sử dụng phần mềm trung gian
[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]26 để xem yêu cầu nào được gửi qua dây
$res = $client->request['GET', '/redirect/3', ['allow_redirects' => false]]; echo $res->getStatusCode[]; // 3025
Ghi chú
Tùy chọn yêu cầu này không hỗ trợ tùy chỉnh tiêu đề Loại nội dung hoặc bất kỳ tùy chọn nào từ hàm json_encode[] của PHP. Nếu bạn cần tùy chỉnh các cài đặt này, thì bạn phải tự chuyển dữ liệu được mã hóa JSON vào yêu cầu bằng cách sử dụng tùy chọn yêu cầu
$client->request['GET', '/get', ['auth' => ['username', 'password']]];6 và bạn phải chỉ định tiêu đề Loại nội dung chính xác bằng cách sử dụng tùy chọn yêu cầu
[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]28
Không thể sử dụng tùy chọn này với
$client->request['GET', '/get', ['auth' => ['username', 'password']]];6,
$client->request['GET', '/get', [ 'auth' => ['username', 'password', 'digest'] ]];2 hoặc
$client->request['GET', '/get', [ 'auth' => ['username', 'password', 'digest'] ]];3
nhiều phần
Tóm tắtĐặt nội dung của yêu cầu thành biểu mẫu nhiều phần/biểu mẫu dữ liệu. TypearrayConstant_______1_______32Giá trị của
$client->request['GET', '/get', [ 'auth' => ['username', 'password', 'digest'] ]];3 là một mảng gồm các mảng kết hợp, mỗi mảng chứa các cặp giá trị khóa sau
[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]
34. [chuỗi, bắt buộc] tên trường biểu mẫu[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]
35. [StreamInterface/resource/string, bắt buộc] Dữ liệu để sử dụng trong phần tử biểu mẫu[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]
28. [mảng] Mảng kết hợp tùy chọn của các tiêu đề tùy chỉnh để sử dụng với phần tử biểu mẫu[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]
37. [chuỗi] Chuỗi tùy chọn để gửi dưới dạng tên tệp trong phần
$res = $client->request['GET', '/redirect/3', ['allow_redirects' => false]]; echo $res->getStatusCode[]; // 3026
Ghi chú
Không thể sử dụng
$client->request['GET', '/get', [ 'auth' => ['username', 'password', 'digest'] ]];3 với tùy chọn
$client->request['GET', '/get', [ 'auth' => ['username', 'password', 'digest'] ]];2. Bạn sẽ cần phải sử dụng cái này hay cái kia. Sử dụng
$client->request['GET', '/get', [ 'auth' => ['username', 'password', 'digest'] ]];2 cho yêu cầu
[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]00 và
$client->request['GET', '/get', [ 'auth' => ['username', 'password', 'digest'] ]];3 cho yêu cầu
[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]02
Không thể sử dụng tùy chọn này với
$client->request['GET', '/get', ['auth' => ['username', 'password']]];6,
$client->request['GET', '/get', [ 'auth' => ['username', 'password', 'digest'] ]];2 hoặc
$client->request['GET', '/get', [ 'auth' => ['username', 'password', 'digest'] ]];4
on_headers
Tóm lượcCó thể gọi được gọi khi đã nhận được các tiêu đề HTTP của phản hồi nhưng phần thân chưa bắt đầu tải xuống
các loại- gọi được
[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]47
Callable chấp nhận một đối tượng
[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]48. Nếu một ngoại lệ được ném bởi khả năng gọi được, thì lời hứa liên quan đến phản hồi sẽ bị từ chối với một
[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]49 bao bọc ngoại lệ đã được ném
Bạn có thể cần biết những tiêu đề và mã trạng thái nào đã được nhận trước khi dữ liệu có thể được ghi vào phần chìm
$res = $client->request['GET', '/redirect/3', ['allow_redirects' => false]]; echo $res->getStatusCode[]; // 3027
Ghi chú
Khi viết trình xử lý HTTP, hàm
[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]50 phải được gọi trước khi ghi dữ liệu vào phần thân của phản hồi
on_stats
Tóm lược[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]51 cho phép bạn có quyền truy cập vào số liệu thống kê chuyển yêu cầu và truy cập chi tiết chuyển cấp thấp hơn của trình xử lý được liên kết với khách hàng của bạn.
[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]51 là một khả năng gọi được gọi khi trình xử lý gửi yêu cầu xong. Cuộc gọi lại được gọi với số liệu thống kê chuyển về yêu cầu, phản hồi nhận được hoặc lỗi gặp phải. Bao gồm trong dữ liệu là tổng thời gian cần thiết để gửi yêu cầucác loại
- gọi được
[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]53
Callable chấp nhận một đối tượng
[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]54
$res = $client->request['GET', '/redirect/3', ['allow_redirects' => false]]; echo $res->getStatusCode[]; // 3028
phát triển
Tóm lượcXác định một chức năng để gọi khi tiến trình chuyển giao được thực hiện
các loại- gọi được
Không có
Hằng số[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]55
Hàm chấp nhận các đối số vị trí sau
- tổng số byte dự kiến sẽ được tải xuống, 0 nếu không xác định
- số lượng byte được tải xuống cho đến nay
- tổng số byte dự kiến sẽ được tải lên
- số lượng byte được tải lên cho đến nay
$res = $client->request['GET', '/redirect/3', ['allow_redirects' => false]]; echo $res->getStatusCode[]; // 3029
Ủy quyền
Tóm lượcTruyền một chuỗi để chỉ định proxy HTTP hoặc một mảng để chỉ định các proxy khác nhau cho các giao thức khác nhau
- chuỗi
- mảng
Không có
Hằng số[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]56
Truyền một chuỗi để chỉ định proxy cho tất cả các giao thức
$res = $client->request['GET', '/redirect/3']; echo $res->getStatusCode[]; // 2000
Truyền một mảng kết hợp để chỉ định proxy HTTP cho các lược đồ URI cụ thể [i. e. , "http", "https"]. Cung cấp cặp giá trị khóa
[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]57 để cung cấp danh sách tên máy chủ lưu trữ không được ủy quyền cho
Ghi chú
Guzzle sẽ tự động điền giá trị này với biến môi trường
[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]58 của môi trường của bạn. Tuy nhiên, khi cung cấp tùy chọn yêu cầu
[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]59, bạn phải cung cấp giá trị
[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]57 được phân tích cú pháp từ biến môi trường
[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]58 [e. g. ,
[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]62]
$res = $client->request['GET', '/redirect/3']; echo $res->getStatusCode[]; // 2001
Ghi chú
Bạn có thể cung cấp các URL proxy chứa lược đồ, tên người dùng và mật khẩu. Ví dụ,
[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]63
truy vấn
Tóm lượcMảng kết hợp của các giá trị chuỗi truy vấn hoặc chuỗi truy vấn để thêm vào yêu cầu
các loại- mảng
- chuỗi
Không có
Hằng số[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]64
$res = $client->request['GET', '/redirect/3']; echo $res->getStatusCode[]; // 2002
Các chuỗi truy vấn được chỉ định trong tùy chọn
[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]65 sẽ ghi đè lên tất cả các giá trị chuỗi truy vấn được cung cấp trong URI của một yêu cầu
$res = $client->request['GET', '/redirect/3']; echo $res->getStatusCode[]; // 2003
giờ đọc đã kết thúc
SummaryFloat mô tả thời gian chờ sẽ sử dụng khi đọc một bodyTypesfloatDefaultDefaults được truyền trực tuyến thành giá trị của cài đặt ini[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]66 PHPConstant_______1_______67
Thời gian chờ áp dụng cho các thao tác đọc riêng lẻ trên nội dung được phát trực tuyến [khi tùy chọn
[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]68 được bật]
$res = $client->request['GET', '/redirect/3']; echo $res->getStatusCode[]; // 2004
bồn rửa
Tóm lượcChỉ định nơi nội dung của phản hồi sẽ được lưu
các loại- chuỗi [đường dẫn đến tệp trên đĩa]
- Tài nguyên
$client->request['GET', '/get', ['auth' => ['username', 'password']]];
7 $client->request['GET', '/get', ['auth' => ['username', 'password']]];
8
Luồng tạm thời PHP
Hằng số[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]71
Truyền một chuỗi để chỉ định đường dẫn đến tệp sẽ lưu trữ nội dung của nội dung phản hồi
$res = $client->request['GET', '/redirect/3']; echo $res->getStatusCode[]; // 2005
Truyền tài nguyên được trả về từ
$client->request['GET', '/get', ['auth' => ['username', 'password']]];7 để viết phản hồi cho luồng PHP
$res = $client->request['GET', '/redirect/3']; echo $res->getStatusCode[]; // 2006
Truyền một đối tượng
$client->request['GET', '/get', ['auth' => ['username', 'password']]];8 để truyền nội dung phản hồi tới một luồng PSR-7 đang mở
$res = $client->request['GET', '/redirect/3']; echo $res->getStatusCode[]; // 2007
Ghi chú
Tùy chọn yêu cầu
[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]74 không được dùng nữa thay vào đó là tùy chọn yêu cầu
[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]75. Cung cấp tùy chọn
[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]74 hiện là bí danh của
[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]75
ssl_key
Tóm lượcChỉ định đường dẫn đến tệp chứa khóa SSL riêng ở định dạng PEM. Nếu mật khẩu được yêu cầu, thì hãy đặt thành một mảng chứa đường dẫn đến khóa SSL trong phần tử mảng đầu tiên, sau đó là mật khẩu cần thiết cho chứng chỉ trong phần tử thứ hai
các loại- chuỗi
- mảng
Không có
Hằng số[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]78
Ghi chú
[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]79 được triển khai bởi trình xử lý HTTP. Điều này hiện chỉ được hỗ trợ bởi trình xử lý cURL, nhưng có thể được hỗ trợ bởi các trình xử lý bên thứ ba khác
suối
Tóm tắtĐặt thànhuse Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\UriInterface; $onRedirect = function[ RequestInterface $request, ResponseInterface $response, UriInterface $uri ] { echo 'Redirecting! ' . $request->getUri[] . ' to ' . $uri . "\n"; }; $res = $client->request['GET', '/redirect/3', [ 'allow_redirects' => [ 'max' => 10, // allow at most 10 redirects. 'strict' => true, // use "strict" RFC compliant redirects. 'referer' => true, // add a Referer header 'protocols' => ['https'], // only allow https URLs 'on_redirect' => $onRedirect, 'track_redirects' => true ] ]]; echo $res->getStatusCode[]; // 200 echo $res->getHeaderLine['X-Guzzle-Redirect-History']; // //first-redirect, //second-redirect, etc... echo $res->getHeaderLine['X-Guzzle-Redirect-Status-History']; // 301, 302, etc...4 để truyền phát phản hồi thay vì tải xuống tất cả phản hồi từ trước. TypesboolDefault
use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\UriInterface; $onRedirect = function[ RequestInterface $request, ResponseInterface $response, UriInterface $uri ] { echo 'Redirecting! ' . $request->getUri[] . ' to ' . $uri . "\n"; }; $res = $client->request['GET', '/redirect/3', [ 'allow_redirects' => [ 'max' => 10, // allow at most 10 redirects. 'strict' => true, // use "strict" RFC compliant redirects. 'referer' => true, // add a Referer header 'protocols' => ['https'], // only allow https URLs 'on_redirect' => $onRedirect, 'track_redirects' => true ] ]]; echo $res->getStatusCode[]; // 200 echo $res->getHeaderLine['X-Guzzle-Redirect-History']; // //first-redirect, //second-redirect, etc... echo $res->getHeaderLine['X-Guzzle-Redirect-Status-History']; // 301, 302, etc...3Constant
[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]82
$res = $client->request['GET', '/redirect/3']; echo $res->getStatusCode[]; // 2008
Ghi chú
Hỗ trợ phản hồi trực tuyến phải được triển khai bởi trình xử lý HTTP được khách hàng sử dụng. Tùy chọn này có thể không được hỗ trợ bởi mọi trình xử lý HTTP, nhưng giao diện của đối tượng phản hồi vẫn giữ nguyên bất kể nó có được trình xử lý hỗ trợ hay không
đồng bộ
Tóm tắtĐặt thành true để thông báo cho trình xử lý HTTP rằng bạn dự định chờ phản hồi. Điều này có thể hữu ích cho việc tối ưu hóa. TypeboolDefaultnoneConstant_______1_______83kiểm chứng
Tóm lượcMô tả hành vi xác minh chứng chỉ SSL của một yêu cầu
- Đặt thành
use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\UriInterface; $onRedirect = function[ RequestInterface $request, ResponseInterface $response, UriInterface $uri ] { echo 'Redirecting! ' . $request->getUri[] . ' to ' . $uri . "\n"; }; $res = $client->request['GET', '/redirect/3', [ 'allow_redirects' => [ 'max' => 10, // allow at most 10 redirects. 'strict' => true, // use "strict" RFC compliant redirects. 'referer' => true, // add a Referer header 'protocols' => ['https'], // only allow https URLs 'on_redirect' => $onRedirect, 'track_redirects' => true ] ]]; echo $res->getStatusCode[]; // 200 echo $res->getHeaderLine['X-Guzzle-Redirect-History']; // //first-redirect, //second-redirect, etc... echo $res->getHeaderLine['X-Guzzle-Redirect-Status-History']; // 301, 302, etc...
4 để bật xác minh chứng chỉ SSL và sử dụng gói CA mặc định do hệ điều hành cung cấp - Đặt thành
use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\UriInterface; $onRedirect = function[ RequestInterface $request, ResponseInterface $response, UriInterface $uri ] { echo 'Redirecting! ' . $request->getUri[] . ' to ' . $uri . "\n"; }; $res = $client->request['GET', '/redirect/3', [ 'allow_redirects' => [ 'max' => 10, // allow at most 10 redirects. 'strict' => true, // use "strict" RFC compliant redirects. 'referer' => true, // add a Referer header 'protocols' => ['https'], // only allow https URLs 'on_redirect' => $onRedirect, 'track_redirects' => true ] ]]; echo $res->getStatusCode[]; // 200 echo $res->getHeaderLine['X-Guzzle-Redirect-History']; // //first-redirect, //second-redirect, etc... echo $res->getHeaderLine['X-Guzzle-Redirect-Status-History']; // 301, 302, etc...
3 để tắt xác minh chứng chỉ [điều này không an toàn. ] - Đặt thành chuỗi để cung cấp đường dẫn đến gói CA nhằm bật xác minh bằng chứng chỉ tùy chỉnh
- bool
- chuỗi
use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\UriInterface; $onRedirect = function[ RequestInterface $request, ResponseInterface $response, UriInterface $uri ] { echo 'Redirecting! ' . $request->getUri[] . ' to ' . $uri . "\n"; }; $res = $client->request['GET', '/redirect/3', [ 'allow_redirects' => [ 'max' => 10, // allow at most 10 redirects. 'strict' => true, // use "strict" RFC compliant redirects. 'referer' => true, // add a Referer header 'protocols' => ['https'], // only allow https URLs 'on_redirect' => $onRedirect, 'track_redirects' => true ] ]]; echo $res->getStatusCode[]; // 200 echo $res->getHeaderLine['X-Guzzle-Redirect-History']; // //first-redirect, //second-redirect, etc... echo $res->getHeaderLine['X-Guzzle-Redirect-Status-History']; // 301, 302, etc...4Hằng số
[ 'max' => 5, 'strict' => false, 'referer' => false, 'protocols' => ['http', 'https'], 'track_redirects' => false ]87
$res = $client->request['GET', '/redirect/3']; echo $res->getStatusCode[]; // 2009
Nếu bạn không cần gói chứng chỉ cụ thể, thì Mozilla cung cấp gói CA thường được sử dụng có thể tải xuống tại đây [do nhà bảo trì cURL cung cấp]. Khi bạn có gói CA sẵn có trên đĩa, bạn có thể đặt "openssl. cafile" PHP ini để trỏ đến đường dẫn đến tệp, cho phép bạn bỏ qua tùy chọn yêu cầu "xác minh". Bạn có thể tìm thấy nhiều chi tiết hơn về chứng chỉ SSL trên trang web cURL