定制 brzuchal/rest-client 二次开发

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

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

brzuchal/rest-client

Composer 安装命令:

composer require brzuchal/rest-client

包简介

A new synchronous HTTP REST client offering a fluent API with the infrastructure of Symfony HttpClient

README 文档

README

The RestClient package is a PHP library that simplifies working with RESTful APIs. It provides an easy way to create and configure HTTP requests, handle responses, and convert JSON data into PHP objects. This documentation will guide you through the main features and usage of the RestClient package.

Whether you're building web services or powerful API clients in your applications, the RestClient package streamlines the process of interacting with RESTful APIs in PHP. It allows you to focus on your application's functionality rather than the intricacies of making HTTP requests and processing responses.

Installation

You can install the RestClient package via Composer:

composer require brzuchal/rest-client

Documentation

Getting Started

To get started with the RestClient package, create a RestClient instance. You can use the RestClient::create method to do this:

use Brzuchal\RestClient\RestClient;

$client = RestClient::create('https://api.example.com/');

Now you have a RestClient instance ready to make HTTP requests.

Making Requests

The RestClient package allows you to create various types of HTTP requests, such as GET, POST, PUT, DELETE, etc. You can use the RestClient instance to create request objects for these methods.

$response = $client->get('/todos/1')
    ->retrieve();
$data = $response->toArray();
$todo = $response->toEntity(Todo::class);

Error Handling

$response = $client->get('/todos/1')
    ->retrieve();

$response->onStatus(404, function ($response) {
    throw new NotFoundException('Resource not found');
});

$response->onStatus(500, function ($response) {
    throw new ServerErrorException('Server error');
});

Symfony Framework

Configuration

To use the RestClient package in a Symfony application, follow these steps:

  1. Register the bundle in your Symfony application by adding the RestClientBundle to the config/bundles.php file:

    // config/bundles.php
    
    return [
        // ...
        Brzuchal\RestClient\RestClientBundle::class => ['all' => true],
    ];
  2. By default, the RestClientBundle uses Symfony configuration to define REST client services. Create a configuration file (e.g., rest_client.yaml) in the config/packages directory of your Symfony project. Here's an example configuration:

    # config/packages/rest_client.yaml
    
    rest_client:
        clients:
            my_rest_client:
                base_uri: 'https://api.example.com/'

    In this configuration, we define a my_rest_client service with a base URI of https://api.example.com/. You can add more client configurations as needed.

Example Symfony Controller

Here's an example Symfony controller that uses the RestClient package to make API requests:

use Brzuchal\RestClient\RestClient;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Contracts\HttpClient\HttpClientInterface;

class TodoController extends AbstractController
{
    public function index(
        #[Autowire(service: 'rest_client.default')]
        RestClientInterface $client,
    ): JsonResponse {
        return $this->json($client->get('/todos')->toArray());
    }
}

Laravel Framework

Configuration

To use the RestClient package in a Laravel application, follow these steps:

  1. Create a configuration file for the RestClient package using the following Artisan command:

    php artisan vendor:publish --tag=config

    This command will generate a rest_client.php configuration file in the config directory of your Laravel project.

    NOTE!: Laravel 8 and later versions should automatically discover the package. For older Laravel versions, you may need to register the service provider manually.

  2. The configuration for the RestClient package in Laravel is similar to Symfony. Here's an example configuration file (config/rest_client.php):

    return [
        'clients' => [
            'my_rest_client' => [
                'base_uri' => 'https://api.example.com/',
            ],
        ],
    ];

    In this configuration, we define a my_rest_client service with a base URI of https://api.example.com/. You can add more client configurations as needed.

Example Laravel Controller

Here's an example Laravel controller that uses the RestClient package to make API requests:

namespace App\Http\Controllers;

use Brzuchal\RestClient\RestClient;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

class TodoController extends Controller
{
    public function index(Request $request): JsonResponse
    {
        return response()->json(
            app('rest_client.default')->get('/todos')->toArray(),
        );
    }
}

License

The RestClient package is open-source software licensed under the MIT License. See the LICENSE file for more information.

brzuchal/rest-client 适用场景与选型建议

brzuchal/rest-client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 565 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 11 月 02 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 brzuchal/rest-client 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 brzuchal/rest-client 我们能提供哪些服务?
定制开发 / 二次开发

基于 brzuchal/rest-client 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-11-02