airmanbzh/pheign

php 的 Feign 实现

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

This package is auto-updated.

Last update: 2024-08-26 21:39:23 UTC


README

Pheign 库使得编写 PHP HTTP 客户端变得更加容易。

(灵感来源于 OpenFeign/feign

安装

可以使用 Composer 安装此库。安装非常简单

$ composer require airmanbzh/pheign

Composer 将将库安装到您的项目目录 vendor/airmanbzh/pheign 中。

使用方法

要进行请求,您需要

创建一个请求类

<?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){}
}

发送请求

// 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>');

注解

方法

命名空间:pheign\annotation\method... 定义请求方法

@GET, @POST, @PUT, @DELETE

目标

命名空间:pheign\annotation\Target

请求端点

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

头部

命名空间:pheign\annotation\Headers

定义自定义头部

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

数据

命名空间:pheign\annotation\Datas

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

选项

命名空间:pheign\annotation\Options

@Options(CURLOPT_SSL_VERIFYHOST=0, CURLOPT_SSL_VERIFYPEER=0)

用于配置 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'
    )
));

关于 Goaop 及其配置的更多信息:goaop/framework