craftrac / paracurl
PHP的curl包装器
v1.0.1
2024-08-30 09:04 UTC
Requires (Dev)
- phpunit/phpunit: ^11.3
README
Paracurl 是一个简单的PHP包装器,用于使用cURL进行HTTP API请求。它提供了一个易于使用的接口来发送 GET、POST 和 PUT 请求,并支持基本认证和基于令牌的认证。
功能
- 支持
GET、POST和PUTHTTP方法。 - 可以通过环境变量轻松配置API端点和凭证。
- 内置基本认证或基于令牌的认证支持。
- 制作API调用时界面简单直观。
安装
要安装Paracurl,您只需使用Composer将其添加到您的项目中即可
composer require cractrac/paracurl
用法
您可以使用多个API配置。如果API需要基本认证,您可以使用以下 USERNAME 和 PASSWORD 环境变量:否则使用 TOKEN 环境变量
PARACURL_<API_NAME>_BASEURL PARACURL_<API_NAME>_USERNAME PARACURL_<API_NAME>_PASSWORD PARACURL_<API_NAME>_TOKEN
示例
// Initialize Paracurl $paracurl = new Paracurl('<API_NAME>', '<endpoint>'); // Send a GET request $response = $paracurl->get(); $responseData = json_decode($response, true); // Send a GET request with url data $data = [ 'key' => 'value' ]; $response = $paracurl->get($data); $responseData = json_decode($response, true); // Send a POST request with body data $data = [ 'key' => 'value' ]; $response = $paracurl->post($data); $responseData = json_decode($response, true); // Send a PUT request with body data $data = [ 'key' => 'value' ]; $response = $paracurl->put($data); $responseData = json_decode($response, true);