nextstage-brasil / ns-http
各种工具的方法和类
1.0.0
2024-09-07 23:12 UTC
Requires
- php: >=7.4
Requires (Dev)
README
vendor/bin/phpunit
Http 类使用 cURL 封装 HTTP 调用。它支持各种 HTTP 方法,如 GET、POST、PUT 和 DELETE,并允许配置头部、参数和 SSL 选项。
方法
call
对指定的 URL 进行 HTTP 调用。
参数
string $url: 要进行的请求的 URL。array $params: 请求参数(可选,默认:[])。string $method: HTTP 方法(GET、POST、PUT、DELETE 等)(可选,默认:'GET')。array $header: HTTP 头部(可选,默认:['Content-Type: application/json'])。bool $ssl: 验证 SSL(可选,默认:true)。int $timeout: 请求超时时间(秒)(可选,默认:30)。
返回
Response: 包含请求响应的对象,包括body: 响应内容。error_code: cURL 错误代码。error: cURL 错误信息。http_code: HTTP 状态代码。headers: 响应头部。url: 有效的请求 URL。
示例用法
// URL for the request
$url = 'https://api.example.com/data';
// Request parameters
$params = [
'key' => 'value',
'another_param' => 'another_value'
];
// Request headers
$headers = [
'Content-Type: application/json',
'Authorization: Bearer your_token_here'
];
$response = Http::call($url, $params, 'GET', $headers);
// Accessing the response
echo $response->getBody(); // Response content
echo $response->getBodyAsObject(); // Response content as JSON Object
echo $response->getBodyAsArray(); // Response content as JSON Array
echo $response->getHttpCode(); // HTTP status code