承接 xpat/xpat-http 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

xpat/xpat-http

最新稳定版本:v1.0.2

Composer 安装命令:

composer require xpat/xpat-http

包简介

Http requests library.

README 文档

README

A simple and intuitive object-oriented HTTP client for PHP. This library enables you to make HTTP requests (GET, POST, PUT, DELETE) with clean, structured code.

Features

  • Object-oriented design.
  • Supports common HTTP methods: GET, POST, PUT, DELETE.
  • Easily configurable headers and request body.
  • Straightforward and reusable components for URL, headers, and body.

Installation

composer require xpat/xpat-http

Usage

GET Request Example

use Xpat\Http\Request\Get;
use Xpat\Http\Request\Headers;
use Xpat\Http\Request\Url;


$response = (
    new Get(
        new Url(
            'localhost',
            8080,
            '/expense-categories'
        ),
        new Headers([
            'Content-Type' => 'application/json',
            'Accept' => 'application/json',
        ])
    )
)->execute();

echo $response->content() . PHP_EOL;
echo $response->statusCode() . PHP_EOL;

POST Request Example

use Xpat\Http\Request\Headers;
use Xpat\Http\Request\JsonBody;
use Xpat\Http\Request\Post;
use Xpat\Http\Request\Url;


$response = (
    new Post(
        new Url(
            'localhost',
            8080,
            '/expense-categories'
        ),
        new Headers([
            'Content-Type' => 'application/json',
            'Accept' => 'application/json',
        ]),
        new JsonBody([
            'name' => 'New Category',
        ])
    )
)->execute();

echo $response->content() . PHP_EOL;
echo $response->statusCode() . PHP_EOL;

PUT Request Example

use Xpat\Http\Request\Headers;
use Xpat\Http\Request\JsonBody;
use Xpat\Http\Request\Put;
use Xpat\Http\Request\Url;


$response = (
    new Put(
        new Url(
            'localhost',
            8080,
            '/expense-categories/4'
        ),
        new Headers([
            'Content-Type' => 'application/json',
            'Accept' => 'application/json',
        ]),
        new JsonBody([
            'id' => 4,
            'name' => 'Grocery',
        ])
    )
)->execute();

echo $response->content() . PHP_EOL;
echo $response->statusCode() . PHP_EOL;

DELETE Request Example

use Xpat\Http\Request\Delete;
use Xpat\Http\Request\Headers;
use Xpat\Http\Request\Url;


$response = (
    new Delete(
        new Url(
            'localhost',
            8080,
            '/expense-categories/4'
        ),
        new Headers([
            'Content-Type' => 'application/json',
            'Accept' => 'application/json',
        ])
    )
)->execute();

echo $response->content() . PHP_EOL;
echo $response->statusCode() . PHP_EOL;

Components

Url

Defines the request URL, including the host, port, and endpoint.

Headers

Manages HTTP headers as key-value pairs.

JsonBody

Formats and encapsulates the request body as JSON.

Response Object

The execute() method returns a response object with the following methods:

  • content(): The content of the HTTP response.
  • statusCode(): The HTTP status code of the response.

JsonObject

The JsonObject class provides a simple and powerful way to work with JSON data in an object-oriented manner. It allows you to parse, query, and manipulate JSON structures using a key-path syntax.

Example

use Xpat\Http\Json\JsonObject;


$json = new JsonObject(
    '{
        "id": 3,
        "amount": 300,
        "description": "Plain ticket",
        "date": "2024-11-27 22:47:21",
        "category": {
            "id": 2,
            "name": "Travel"
        }
    }'
);

if ($json->has('category.name')) {
    echo $json->get('category.name');
} else {
    echo 'No category name found';
}

License

The MIT License (MIT)

Copyright (c) 2024 Alymbek Kydyrmyshev

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-12-11