wesley0010012/http-request 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱: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.

统计信息

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

GitHub 信息

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

其他信息

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

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固