Download the PHP package zhanguangcheng/php-http-client without Composer
On this page you can find all versions of the php package zhanguangcheng/php-http-client. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download zhanguangcheng/php-http-client
More information about zhanguangcheng/php-http-client
Files in zhanguangcheng/php-http-client
Package php-http-client
Short Description A simple and powerful HTTP client library for PHP.
License MIT
Informations about the package php-http-client
HTTP client library for PHP
README.md | 中文 README.md
A simple and powerful HTTP client library for PHP.
Features
- Send GET, POST, PATCH, PUT, DELETE, etc. requests
- File upload and download
- Request retry
- cURL handle reuse, improve performance
- Concurrent requests
- Cookie keep
- gzip support
- User authentication
File Structure
Installation
Requirements
- PHP >= 8.0
- curl extension
- json extension
Basic Usage
Configuration
HttpClient contains many options that control how requests are executed, including retry, concurrency, proxy, authentication, cookie keep, etc. These options can be defined globally (apply to all requests) and per request (override any global options).
You can create a client with options using HttpClient::create($options)
, $options
is global options.
or, combined with the getter and setter of the Options class:
Send a request with options:
Making Requests
Sending Different Methods
Query String Parameters
You can manually add them to the URL that will be added to the request, or you can define them as an associative array with the Options::QUERY
option, which will be merged with the URL:
Sending Request Headers
You can define the request headers to be sent with the Options::HEADERS
option:
Sending Request Body
Use the second parameter of the post()
, put()
, patch()
, delete()
methods to send the request body:
You can also use the Options::BODY
option to define the request body to be sent:
When uploading data using the POST method, if you do not explicitly define the Content-Type request header, the Content-Type:application/x-www-form-urlencoded
request header will be added by default.
If you want to customize the request type, you can use the Options::CONTENT_TYPE
option, for example using the JSON format:
Gzip Compression
If the zlib extension is installed, the request header Accept-Encoding: gzip
will be sent by default.
When getting the response content, if the server supports gzip compression and the response header contains Content-Encoding: gzip
, the response content will be automatically decompressed.
You can also turn off gzip compression:
Redirects
By default, the HTTP client will track redirects when making requests, and track 5 redirects by default. Use the Options::MAX_REDIRECTS
setting to configure this behavior:
Retry Failed Requests
Sometimes, requests fail due to network issues or temporary server errors. You can use the Options::MAX_RETRY
option to automatically retry failed requests.
By default, failed requests are retried up to 3 times, with a delay between retries of 1 second for the first retry; 3 seconds for the second retry; and 5 seconds for the third retry. The conditions for retrying are: request timeout or
response status code is one of 423, 425, 429, 500, 502, 503, 504, 507 and 510.
User Authentication
HttpClient supports different authentication mechanisms. They can be defined globally in the configuration (apply to all requests) and per request (override any global authentication):
Request Proxy
HttpClient supports sending requests using HTTP proxies. They can be defined globally in the configuration (apply to all requests) and per request (override any global proxy):
Cookie Keep
Use the CookieJar class to keep the cookies in the response and send them in subsequent requests:
File Download
Use the download()
method of HttpClient
to download files, and you can use the Options::ON_PROGRESS
option to monitor the download progress:
HTTPS Certificate Verification
Certificate download address: https://curl.haxx.se/docs/caextract.html
By default, the system's CA certificate is used, such as the curl.cainfo
or openssl.cafile
configuration in the php configuration file:
You can also use the Options::CAFILE
option to specify the HTTPS certificate:
Close certificate verification (not recommended in production environment):
Concurrent Requests
Set the second parameter of HttpClient::create($options, $concurrency)
to the number of concurrent requests, use the addRequest($method, $url, $options)
method to add requests, and then use the send()
method to send requests, the
response array is in the same order as the request array.
You can also use the Options::MAX_RETRY
option to set the failed requests when retrying concurrent requests.
Processing Responses
All responses returned by HttpClient are objects of type Response and provide the following methods:
Testing
Run the following command to run the tests:
100% code coverage
References
All versions of php-http-client with dependencies
ext-curl Version *
ext-json Version *