定制 wesley0010012/http-request 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

wesley0010012/http-request

最新稳定版本:3.1.0

Composer 安装命令:

composer require wesley0010012/http-request

包简介

Simple HttpRequest Interface

README 文档

README

Description

The HttpRequest provides a simple interface for making HTTP requests. This facade encapsulates the complexity of creating HTTP requests, allowing intuitive GET, POST, PUT, PATCH, and DELETE calls.

Installation

If you are using Composer, add the dependency to your project:

composer require wesley0010012/http-request

Usage

Every request is maked using HttpRequestFacade.

Before making any requests, you can set the HttpRequestFacade to the singleton mode, preventing creation of some dependencies before every request:

use Wesley0010012\HttpRequest\Facades\HttpRequestFacade;

HttpRequestFacade::singleton();

Making a GET Request

$response = HttpRequestFacade::get('https://api.example.com/data');

echo $response->getStatusCode()->value;
echo $response->getBody();

Making a POST Request

$response = HttpRequestFacade::post(
    'https://api.example.com/users',
    json_encode(['name' => 'John', 'email' => 'john@email.com']),
    ['Content-Type' => 'application/json']
);

echo $response->getStatusCode()->value;
echo $response->getBody();

Making a PUT Request

$response = HttpRequestFacade::put(
    'https://api.example.com/users/1',
    json_encode(['name' => 'John Doe']),
    ['Content-Type' => 'application/json']
);

echo $response->getStatusCode()->value;
echo $response->getBody();

Making a PATCH Request

$response = HttpRequestFacade::patch(
    'https://api.example.com/users/1',
    json_encode(['email' => 'new@email.com']),
    ['Content-Type' => 'application/json']
);

echo $response->getStatusCode()->value;
echo $response->getBody();

Making a DELETE Request

$response = HttpRequestFacade::delete('https://api.example.com/users/1');

echo $response->getStatusCode()->value;
echo $response->getBody();

Method Parameters

All request methods follow the same signature:

HttpRequestFacade::{method}(string $url, mixed $body = null, array $headers = [], int $timeout = 30): HttpResponse
  • (string)`: The request URL.
  • (*mixed*): The request body, optional for GET and DELETE.
  • (*array*): HTTP headers.
  • (*int*): Request timeout in seconds (default: 30).

Response

Each request returns an HttpResponse object, which can be used to access:

  • getStatusCode(): Returns the HTTP status code of the response.
  • getBody(): Returns the response body or null if not exists.

Statuses

Each HTTP status exposes the following methods to accurately classify the nature of the response:

  • isInformational() (bool): Return if is a Informational status;
  • isSuccess() (bool): Return if is a Success status;
  • isRedirection() (bool): Return if is a Redirection status;
  • isClientError() (bool): Return if is a Client Error status;
  • isServerError() (bool): Return if is a Server Error status;

Example Usage:

echo $response->getStatusCode()->value; // 200
echo $response->getBody(); // "{\"message\": \"Success\"}"

License

This project is licensed under the MIT License - see the LICENSE file for details.

统计信息

  • 总下载量: 67
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-03-29