hraw/httpclient

PHP cURL 的简单易实现的包装器

v1.0.0 2022-06-08 15:05 UTC

This package is auto-updated.

Last update: 2024-09-08 19:54:46 UTC


README

PHP cURL 的简单易实现的包装器。

需求

  • PHP 7.0 或更高版本
  • Composer 用于安装

支持的方法

  • GET
  • POST
  • PUT
  • PATCH
  • DELETE

安装

composer require hraw/httpclient

实现

<?php

use Curl\HttpClient;

$response = HttpClient::get('https://jsonplaceholder.typicode.com/todos')->send();

响应时,它返回 CurlResponse 类的实例。

<?php

//Get the data returned by api
$response->data();

//Get status code
$response->statusCode();

//Check if response received is success response or not
$response->success();

//Get all the properties returned in response
$response->properties();

您还可以在执行 cURL 请求时添加头信息和超时值

HttpClient::get($url)
            ->timeout(60)
            ->headers([
                'Content-Type : application/json',
                'Authorization : Bearer {token}'
            ])
            ->send();

POST 请求

HttpClient::post($url, $data)->send();
  • PUT 和 PATCH 方法与 POST 方法的实现相同。
  • DELETE 方法的实现与 GET 方法相同。