molchanoviv/retrofit-php

a fork of tebru/retrofit-php

v2.8.3 2016-09-12 14:47 UTC

README

Build Status Coverage Status Scrutinizer Code Quality SensioLabsInsight

This library aims to ease creation of REST clients. It is blatantly stolen from square/retrofit and implemented in PHP.

Overview

Retrofit allows you to define your REST API with a simple interface.

<?php

use Tebru\Retrofit\Annotation as Rest;

interface GitHubService
{
    /**
     * @Rest\GET("/users/{user}/list")
     * @Rest\Returns("ArrayCollection<ListRepo>")
     */
    public function listRepos($user);
}

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

<?php

use Tebru\Retrofit\Adapter\RestAdapter;

$restAdapter = RestAdapter::builder()
    ->setBaseUrl('https://api.github.com')
    ->build();
    
$gitHubService = $restAdapter->create(GitHubService::class);

Our newly created service is capable of making GET requests to /users/$user/list to return an ArrayCollection of ListRepo objects.

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

Usage examples are referenced from Square's documentation

Installation & Usage

composer require tebru/retrofit-php

Please make sure you also install an http client. Currently guzzle is the only supported option

composer require guzzlehttp/guzzle

Documentation

License

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