benatespina/stack-exchange-api-client

StackExchange v2.2 API 客户端

v0.7.1 2015-09-29 06:34 UTC

This package is not auto-updated.

Last update: 2024-09-14 15:40:36 UTC


README

PHP 库,用于与 Stack Exchange 的 2.2 版 REST API 交互。

SensioLabsInsight Build Status Scrutinizer Code Quality Total Downloads Latest Stable Version Latest Unstable Version

要求

PHP >= 7.1

安装

安装此软件包最简单的方法是使用 Composer

$ composer require benatespina/stack-exchange-api-client

用法

如果您查看 API 文档,您会看到有一些调用不需要身份验证,但无论如何,还有其他调用需要身份验证,因此,这个库允许您使用或不需要身份验证来实例化 StackExchange 客户端。此外,还有两种使用此库的方法。

默认用法

<?php

require_once __DIR__ . '/../vendor/autoload.php';

use BenatEspina\StackExchangeApiClient\StackExchange;

$client = StackExchange::withAuth('stack-exchange-key', 'stack-exchange-access-token');

$profile = $client->user()->me();
$answers = $client->answer()->answers();
$answer = $client->answer()->createAnswer('the-question-id', 'This is my awesome answer!');


$client = StackExchange::withoutAuth();

// Throws an AuthenticationIsRequired exception
$profile = $client->user()->me();

$answers = $client->answer()->answers();

// Throws an AuthenticationIsRequired exception
$answer = $client->answer()->createAnswer('the-question-id', 'This is my awesome answer!');

完全自定义用法

<?php

require_once __DIR__ . '/../vendor/autoload.php';

use BenatEspina\StackExchangeApiClient\Authentication\Authentication;
use BenatEspina\StackExchangeApiClient\Http\GuzzleHttpClient;
use BenatEspina\StackExchangeApiClient\Model\Answer;
use BenatEspina\StackExchangeApiClient\Serializer\ToModelSerializer;
use BenatEspina\StackExchangeApiClient\StackExchange;

$httpClient = new GuzzleHttpClient();
$authentication = new Authentication('stack-exchange-key', 'stack-exchange-access-token');

$client = new StackExchange($httpClient, $authentication);

$profile = $client->user()->me();
$answers = $client->answer()->answers();
$answer = $client->answer()->createAnswer('the-question-id', 'This is my awesome answer!');


// CUSTOM SERIALIZATION
//
// Instantiate the AnswerApi with custom serializer
// The following calls returns an answer model instance instead of a plain array

$answersApi = $client->answer(new ToModelSerializer(Answer::class));
$answers = $answersApi->answers();
$answer = $answersApi->createAnswer('the-question-id', 'This is my awesome answer!');

当前状态

API 有很多方法,因此调用实现状态按以下文件 类型 分开

测试

此软件包由 PHPSpec(PHP的SpecBDD框架) 完全测试。

运行以下命令以启动测试

$ vendor/bin/phpspec run -fpretty

贡献

此软件包遵循PHP编码标准,因此需要执行Fabien Potencier的 PHP-CS-Fixer。此外,如果PR创建了某些非PHP文件,请记住您需要手动添加许可证头。为了简化,我们提供了一组Composer脚本,这些脚本封装了与此过程相关的所有命令。

$ composer run-script cs

还有关于为此软件包贡献的政策。拉取请求必须逐步解释,以便使审查过程简单,以便接受和合并它们。新的方法或代码改进必须与 PHPSpec 测试配对。

致谢

此库由以下人员创建

@benatespina - benatespina@gmail.com

许可选项

License