shewa12/wp-http-client
最新稳定版本:v1.0.0
Composer 安装命令:
composer require shewa12/wp-http-client
包简介
A wrapper class for making HTTP requests using WordPress wp_remote_request API's.
README 文档
README
The wp-http-client is a PHP composer package to make HTTP requests using the WordPress API. It provides methods for making GET, POST, PUT, and DELETE requests with ease. This class is especially useful for developers who are working on WordPress plugins or themes and need to interact with external APIs or remote services.
Installation
composer require shewa12/wp-http-client
Instantiating the HTTPClient Class
To use the wp-http-client, create an instance of it as follows:
use Shewa\WP_HTTP_Client\HTTPClient; // Instantiate the HTTPClient class $http_client = new HTTPClient();
Usage
request() method is responsible for performing http requests. This method accept four params. These are:
- $request, required. Which request need to make. Supported values: get, post, put, patch, delete.
- $url, required. Where to make request
- $data required if request is not get or delete
- $args optional, pass to override default values
Example of get request:
$url = 'https://api.example.com/data'; $response = $http_client->request( 'get', $url ); if ( ! is_wp_error( $response ) ) { // Process the response data print_r( $response ); } else { // Handle the error echo 'Error: ' . $response->get_error_message(); }
Example of post request:
$url = 'https://api.example.com/data'; $data = ['name' => 'John']; $response = $http_client->request( 'post', $url, $data ); if ( ! is_wp_error( $response ) ) { // Process the response data print_r( $response ); } else { // Handle the error echo 'Error: ' . $response->get_error_message(); }
Response
Response will be either WP_Error or Array.
Successful response will return array like below:
$response = array( 'headers' => [], 'code' => 200, 'message' => 'OK', 'body' => [], );
Methods
- REQUEST
- GET
- POST
- PUT
- PATCH
- DELETE
Supported HTTP Arguments & Default Values
$defaults = array( 'method' => 'GET', 'timeout' => 5, 'redirection' => 5, 'httpversion' => 1.0, 'user-agent' => 'WordPress/' . get_bloginfo( 'version' ) . '; ' . get_bloginfo( 'url' )', 'reject_unsafe_urls' => false, 'blocking' => true, 'headers' => array(), 'cookies' => array(), 'body' => null, 'compress' => false, 'decompress' => true, 'sslverify' => true, 'sslcertificates' => ABSPATH . WPINC . '/certificates/ca-bundle.crt', 'stream' => false, 'filename' => null, 'limit_response_size' => null, );
统计信息
- 总下载量: 14
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2023-08-06