airmanbzh/pheign

Feign implement for php

v1.0 2017-08-23 20:58 UTC

This package is auto-updated.

Last update: 2022-09-26 17:42:41 UTC


README

Pheign library makes writing php http client easier.

(inspired by OpenFeign/feign)

Installation

Library can be installed with Composer. Installation is quite easy:

$ composer require airmanbzh/pheign

Composer will install the library to your project's vendor/airmanbzh/pheign directory.

Usage

To make a request, you have to :

Create a request class

<?php
namespace test;

use pheign\annotation\method\GET;
use pheign\annotation\Options;
use pheign\annotation\Pheign;
use pheign\annotation\Target;

class Github
{
    /**
     * @Pheign
     *
     * @GET
     * @Target("/users/{owner}/repos")
     *
     * @Options(CURLOPT_SSL_VERIFYHOST=0, CURLOPT_SSL_VERIFYPEER=0)
     */
    public function repositories($owner){}
    
    /**
     * @Pheign
     *
     * @GET
     * @Target("/repos/{owner}/{repo}")
     *
     * @Options(CURLOPT_SSL_VERIFYHOST=0, CURLOPT_SSL_VERIFYPEER=0)
     */
    public function repositoryInformations($owner, $repo){}
}

Make a request

// Initialisation de pheign
$pheign = \pheign\builder\Pheign::builder()->target(\test\Github::class, 'https://api.github.com');

$result = $pheign->repositories('airmanbzh');
echo('<pre>' . htmlentities($result) . '</pre>');

$result = $pheign->repositoryInformations('airmanbzh', $repo);
echo('<pre>' . htmlentities($result) . '</pre>');

Annotations

Method

Namespace : pheign\annotation\method... Define request method

@GET, @POST, @PUT, @DELETE

Target

Namespace : pheign\annotation\Target

Request endpoint

@Target("/search/{id}")

Headers

Namespace : pheign\annotation\Headers

Define a custom header

@Headers({"Content-Type : application/json", "Accept-Charset: utf-8"})

Datas

Namespace : pheign\annotation\Datas

@Datas(myDatas="{datas}", myId="{id}")

Options

Namespace : pheign\annotation\Options

@Options(CURLOPT_SSL_VERIFYHOST=0, CURLOPT_SSL_VERIFYPEER=0)

To configure AOP

<?php
$loader = require_once(__DIR__ . '/../vendor/autoload.php');

$applicationAspectKernel = \pheign\kernel\PheignKernel::getInstance();
$applicationAspectKernel->init(array(
    'debug' => true,
    'appDir' => __DIR__ . '/../private', // The directory where you find your request class
    'cacheDir' => __DIR__ . '/../cache',
    'excludePaths' => array(
        __DIR__ . '/../vendor'
    )
));

More about Goaop and its configuration : goaop/framework