axguowen/http-client

Simple HTTP Client Library for PHP

v1.0.1 2024-02-18 08:45 UTC

This package is auto-updated.

Last update: 2024-04-18 12:35:01 UTC


README

一个简单的HTTP客户端工具

主要功能:

发送请求

安装

composer require axguowen/http-client

用法示例

发送get请求

// 请求url
$url = 'https://domain/path/';
// header参数
$headers = [
    'Content-Type' => 'application/json;charset=utf-8'
];
// 发送请求
$ret = \axguowen\HttpClient::post($url, $headers);
if (!$ret->ok()) {
    return [null, new \axguowen\httpclient\Error($path, $ret)];
}
$r = ($ret->body === null) ? [] : $ret->json();

发送post请求

// 请求url
$url = 'https://domain/path/';
// 请求体
$body = [
    'id' => 1,
    'page' => 2,
];
// header参数
$headers = [
    'Content-Type' => 'application/json;charset=utf-8'
];
// 发送请求
$ret = \axguowen\HttpClient::post($url, $body, $headers);
if (!$ret->ok()) {
    return [null, new \axguowen\httpclient\Error($path, $ret)];
}
$r = ($ret->body === null) ? [] : $ret->json();