codeat3/foaas-client

FOAAS服务的PHP客户端

1.0.2 2020-01-31 12:11 UTC

This package is auto-updated.

Last update: 2024-08-29 05:07:03 UTC


README

Latest Version on Packagist Build Status Quality Score Total Downloads

FOAAS的PHP客户端 FOAAS

安装

您可以通过composer安装此包

composer require codeat3/foaas-client

用法

客户端的基本用法

use Codeat3\FoaasClient\FoaasClient;

$foaasClient = new FoaasClient([
    'decency' => 'low', // possible decency filter options are 'low', 'medium', 'high', 'extreme' - default is no filter
]);
echo $foaasClient->what('John')->get();

// Output
What the f*ck‽ - John

使用格式获取您需要的响应

use Codeat3\FoaasClient\FoaasClient;

$foaasClient = new FoaasClient([
    'decency' => 'low',
    'responseAs' => 'array', // possible response formats are 'text' (default), 'html', 'xml', 'json', 'array'
]);
print_r($foaasClient->what('John')->get());

// Output
Array
(
    [message] => What the f*ck‽
    [subtitle] => - John
)

还提供了一些辅助函数,用于获取预期输出的类型

$foaasClient = new FoaasClient([
	'decency' => 'low',
]);
echo $foaasClient->what('John')->getAsText(); // What the f*ck‽ - John
echo $foaasClient->what('John')->getAsXml(); // <?xml version="1.0" encoding="UTF-8"?> <foaas:response xmlns:foaas="http://foaas.com/f*ckoff"> <foaas:message>What the f*ck‽</foaas:message> <foaas:subtitle>- John</foaas:subtitle> </foaas:response>
echo $foaasClient->what('John')->getAsHtml(); // <!DOCTYPE html> <html> <head> <title>FOAAS - What the f*ck‽ - John</title> <meta charset="utf-8"> <meta property="og:title" content="What the f*ck‽ - John"> ...
echo $foaasClient->what('John')->getAsJson(); // {"message":"What the f*ck‽","subtitle":"- John"}

print_r($foaasClient->what('John')->getAsArray());
/*
Array
(
    [message] => What the f*ck‽
    [subtitle] => - John
)
*/
自定义响应

您可以实现自定义响应并将其传递给客户端,以满足您的需求

// Implementation
class ObjectResponse implements FoaasResponse
{
    protected $acceptHeader = 'application/json';

    public function getHeaders():string
    {
        return $this->acceptHeader;
    }

    public function response(string $response, FoaasFilter $filter)
    {
        $response = $filter->filter($response);
        return json_decode($response);
    }
}

// Use
$foaasClient = new FoaasClient([
    'decency' => 'low',
    'responseAs' => 'object',
    'responseFormats' => [
        'object' => ObjectResponse::class,
    ]
]);
var_dump($foaasClient->what('John')->get());

/*
class stdClass#27 (2) {
  public $message =>
  string(16) "What the f*ck‽"
  public $subtitle =>
  string(6) "- John"
}
*/

测试

composer test

变更日志

有关最近更改的更多信息,请参阅CHANGELOG

贡献

有关详细信息,请参阅CONTRIBUTING

安全

如果您发现任何与安全相关的问题,请通过电子邮件swapnilsarwe@gmail.com联系,而不是使用问题跟踪器。

致谢

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件

PHP包模板

此包是使用PHP包模板生成的。