Pheign library makes writing php http client easier.
(inspired by OpenFeign/feign)
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.
To make a request, you have to :
<?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>');
Namespace : pheign\annotation\method... Define request method
@GET, @POST, @PUT, @DELETE
Namespace : pheign\annotation\Target
Request endpoint
@Target("/search/{id}")
Namespace : pheign\annotation\Headers
Define a custom header
@Headers({"Content-Type : application/json", "Accept-Charset: utf-8"})
Namespace : pheign\annotation\Datas
@Datas(myDatas="{datas}", myId="{id}")
Namespace : pheign\annotation\Options
@Options(CURLOPT_SSL_VERIFYHOST=0, CURLOPT_SSL_VERIFYPEER=0)
<?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