PHP library for interacting with the Stack Exchange's version 2.2 REST API.
PHP >= 7.1
The easiest way to install this bundle is using Composer
$ composer require benatespina/stack-exchange-api-client
If you check out the API documentation, you will see that there are some calls that do not need authentication, but nevertheless there are other calls that need it so, this library allows to instantiate StackExchange client with or without Authentication. Furthermore there two ways to use this library.
<?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!');
The API has many methods, so the calls' implementation status are separated by type in the following files:
Access Tokens
Answers
Badges
Comments
Errors
Events
Filters
Flag Options
Inbox Items
Network Users
Merge History
Network Activity
Notifications
Posts
Privileges
Questions
Question Timelines
Reputation
Reputation History
Revisions
Search Excerpts
Sites
Suggested Edits
Tags
Tag Scores
Tag Synonyms
Tag Wikis
Top Tags
Users
User Timeline
Write Permissions
This bundle is completely tested by PHPSpec, SpecBDD framework for PHP.
Run the following command to launch tests:
$ vendor/bin/phpspec run -fpretty
This bundle follows PHP coding standards, so pull requests need to execute the Fabien Potencier's PHP-CS-Fixer. Furthermore, if the PR creates some not-PHP file remember that you have to put the license header manually. In order to simplify we provide a Composer script that wraps all the commands related with this process.
$ composer run-script cs
There is also a policy for contributing to this bundle. Pull requests must be explained step by step to make the review process easy in order to accept and merge them. New methods or code improvements must come paired with PHPSpec tests.
This library is created by:
@benatespina - [email protected]