fuziion / http-api-client
此PHP库提供了一个简单且功能丰富的HTTP API客户端,用于与API交互。它允许您向API发送GET和POST请求,并包括数据处理功能,便于处理API响应数据。
dev-master
2023-10-12 15:42 UTC
Requires
- ext-curl: *
Requires (Dev)
- phpunit/phpunit: ^10.4
This package is auto-updated.
Last update: 2024-09-13 11:03:35 UTC
README
此PHP库提供了一个简单且功能丰富的HTTP API客户端,用于与API交互。它允许您向API发送GET和POST请求,并包括数据处理功能,便于处理API响应数据。
安装
您可以使用Composer安装此库
composer require fuziion/http-api-client
使用方法
要使用Fuziion\HttpApi\Client
类,请按照以下步骤操作
导入必要的命名空间
use Fuziion\HttpApi\Client;
通过提供API密钥和API URL创建客户端实例
$client = new Client('your-api-key', 'https://api.example.com');
向API发送GET或POST请求
GET请求
$response = $client->get('/some/endpoint'); if ($response) { // Access API response data using magic getters echo $response->some_key; // Access 'some_key' in the response data // Or get the entire data array $data = $response->getData(); print_r($data); } else { // Handle the error echo "Error making the API GET request."; }
POST请求
$data = [ 'key1' => 'value1', 'key2' => 'value2', ]; $response = $client->post('/some/endpoint', $data); if ($response) { // Access API response data using magic getters echo $response->some_key; // Or get the entire data array $data = $response->getData(); print_r($data); } else { // Handle the error echo "Error making the API POST request."; }
将'your-api-key'
和'https://api.example.com'
替换为您实际的API密钥和URL。如果您尝试访问的API不需要bearer token,则只需留此字段为空。
数据对象
库包括一个用于处理API响应数据的DataObject类,具有魔法获取器。它简化了在API响应中访问数据的过程。
use Fuziion\HttpApi\Data\DataObject; $data = new DataObject(['key1' => 'value1', 'key2' => 'value2']);
echo $data->key1; // Access 'key1' in the data object
许可证
此库采用MIT许可证。有关详情,请参阅LICENSE文件。