retrofit php接口,Retrofit - PHP的REST API 客户端创建库

"RetrofitPHPRetrofit是一个基于PHP实现的类型安全的REST客户端,灵感来源于square/retrofit。升级到版本3需要注意重大变化,需要安装Guzzle进行HTTP请求和Gson进行序列化转换。通过简单的接口定义,Retrofit可以方便地创建和执行REST API请求,如GET请求到/users/{user}
摘要由CSDN通过智能技术生成

Retrofit PHP

68747470733a2f2f7472617669732d63692e6f72672f74656272752f726574726f6669742d7068702e7376673f6272616e63683d6d617374657268747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f74656272752f726574726f6669742d7068702f6261646765732f636f7665726167652e706e673f623d6d617374657268747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f74656272752f726574726f6669742d7068702f6261646765732f7175616c6974792d73636f72652e706e673f623d6d617374657268747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f64323138386266382d383234382d346466362d386263352d3831353066633062383839382f6d696e692e706e67

Retrofit is a type-safe REST client. It is blatantly stolen from square/retrofit and implemented in PHP.

❗UPGRADE NOTICE

Version 3 introduces many breaking changes. Please review the upgrade guide before upgrading.

Overview

The following is for version 3, please check out the corresponding tag for version 2 documentation

Retrofit allows you to define your REST API with a simple interface. The follow example will attempt to display a typical use-case, but requires two additional libraries. The first uses Guzzle to make http requests as Retrofit does not ship with any default way to make network requests. The second uses a serializer (Gson) to hook into Retrofit's Converter functionality. This allows for automatic serialization of request bodies and deserialization of response bodies.

interface GitHubService

{

/**

* @GET("/users/{user}/list")

* @Path("user")

* @ResponseBody("App\GithubService\ListRepo")

* @ErrorBody("App\GitHubService\ApiError")

*/

public function listRepos(string $user): Call;

}

Annotations are used to configure the endpoint. Then, the Retrofit class generates a working implementation of the service interface.

$retrofit = Retrofit::builder()

->setBaseUrl('https://api.github.com')

->setHttpClient(new Guzzle6HttpClient(new Client())) // requires a separate library

->addConverterFactory(new GsonConverterFactory(Gson::builder()->build())) // requies a separate library

->build();

$gitHubService = $retrofit->create(GitHubService::class);

Our newly created service is capable of making GET requests to /users/{user}/list, which returns a Call object.

$call = $gitHubService->listRepos('octocat');

The Call object is then used to execute the request synchronously or asynchronously, returning a response.

$response = $call->execute();

// or

$call->enqueue(

function(Response $response) { }, // response callback (optional)

function(Throwable $throwable) { } // error callback (optional)

);

$call->wait();

You can then check to see if the request was successful and get the deserialized response body.

if (!$response->isSuccessful()) {

throw new ApiException($response->errorBody());

}

$responseBody = $response->body();

Usage examples are referenced from Square's documentation

Installation & Usage

Retrofit 3 requires PHP 7.1

composer require tebru/retrofit-php

Please make sure you also install an http client.

composer require tebru/retrofit-php-http-guzzle6

Install a converter to handle more advanced request and response body conversions.

composer require tebru/retrofit-php-converter-gson

Documentation

License

This project is licensed under the MIT license. Please see the LICENSE file for more information.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值