1.0.1 2019-12-11 03:14 UTC

This package is auto-updated.

Last update: 2024-09-12 08:00:51 UTC


README

Build Status Maintainability Test Coverage

简单的 PHP curl 包装器

这个轻量级的 curl 包装器简化了最常见的需求,并专为 OAuth 流、使用 JSON 响应的 API 等服务器到服务器的通信设计。

安装

composer require neoan3-apps/curl

快速入门

Json 响应会自动解码为关联数组。

try{
    $comments = \Neoan3\Apps\Curl::get('https://jsonplaceholder.typicode.com/posts/1/comments');
} catch (CurlException $e){
    echo $e->getMessage();
}


/* output
* [
*   ['postId' => 1, 'name' => 'labore ...']
*   [...]
* ]
*
*/

简化调用

get($url, $array = [], $auth = false, $authType = 'Bearer')

注意: $array 转换为 GET 参数

post($url, $array = [], $auth = false, $authType = 'Bearer')

put($url, $array = [], $auth = false, $authType = 'Bearer')

这些调用最常见,可以用或不用授权。如果设置了 $auth,则方法假定 Baerer 令牌。

curling($url, $arrayOrBody, $header, $type = 'POST')

自定义调用,其中手动设置 header 为数组,方法默认为 POST

setResponseFormatVerbose()

该类默认仅输出 "plain",只包含响应的有效负载。此方法将行为更改为以下格式的响应

[
    'headers' => $headers, // array
    'body' => $responseBody, // array
    'status' => $status // int (e.g.200)
];

在输出格式之间切换时,请使用 setResponseFormatPlain() 重置行为。

异常

CurlException 仅在响应为 500 及以上时抛出。这意味着 404 是一个有效的调用,应该在其他地方进行有用性的评估。

安全:虽然我相信您的供应商文件夹可能已经受到保护,但 neoan3-curl 使用 .htaccess 文件保护 _log 文件夹。如果您不使用 Apache 且供应商文件夹可见,请采取措施保护 vendor/neoan3-apps/curl/_log 文件夹。