craftrac/paracurl

PHP的curl包装器

v1.0.1 2024-08-30 09:04 UTC

This package is auto-updated.

Last update: 2024-09-30 09:19:12 UTC


README

Paracurl 是一个简单的PHP包装器,用于使用cURL进行HTTP API请求。它提供了一个易于使用的接口来发送 GETPOSTPUT 请求,并支持基本认证和基于令牌的认证。

功能

  • 支持 GETPOSTPUT HTTP方法。
  • 可以通过环境变量轻松配置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);