benatespina/stack-exchange-api-client

StackExchange v2.2 API client


Keywords
api, client, library, stackExchange
License
MIT

Documentation

stackExchange-logo Stack Exchange v2.2 API Client

PHP library for interacting with the Stack Exchange's version 2.2 REST API.

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

This is a work in progress library and some features are not available yet.

Installation

The recommended and the most suitable way to install is through Composer. Be sure that the tool is installed in your system and execute the following command:

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

Usage

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 the authentication.

First of all, you have to instantiate the Client that to be used like a parameter of the constructor of the API objects. And then, after create the API object, simply you have to call the different methods that this object offers.

The only difference between with or without authentication is the Client constructor. Without authentication the constructor used the null value by default; otherwise, with authentication, firstly, you have to construct the OAuth object that then it passed as parameter in Client constructor.

Without authentication:

$client = new Client();
$answerAPI = new AnswerAPI($client);

$answers = $answerAPI->getAnswersById(['2359967', '1932551']);

The second parameter has been omitted, because the method getAnswersById already contains by default the minimum params to do a proper request: ['site' => 'stackoverflow', 'sort' => 'activity']

With authentication:

There are two variants to construct the OAuth2 object:

The first one directly passed the $key and $accessToken.

$oauth2 = new OAuth2($key, $accessToken);

$client = new Client($oauth2);
$questionAPI = new QuestionAPI($client);

$question = $questionAPI->postQuestion('The title of question', 'The body of the question', ['php', 'api']);

But the recommended variant is the variant that passes the $key, $clientId, $scope, $redirectUri and the getAccessToken(), and returns the token.

$oauth2 = new OAuth2($key, null, $clientId, $scope, $redirectUri);

$client = new Client($oauth2);
$questionAPI = new QuestionAPI($client);

$question = $questionAPI->postQuestion('The title of question', 'The body of the question', ['php', 'api']);

Current status

This API has many methods, so the status of the calls are separated by type in the following files:

Tests

This library is completely tested by PHPSpec, SpecBDD framework for PHP.

If you want to run the tests, only you have to run the following command:

$ vendor/bin/phpspec run -fpretty

Contributing

This project follows PHP coding standards, so pull requests need to execute the Fabien Potencier's PHP-CS-Fixer and Marc Morera's PHP-Formatter. 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 project. 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.

If you would like to contribute it is a good point to follow Symfony contribution standards, so please read the Contributing Code in the project documentation. If you are submitting a pull request, please follow the guidelines in the Submitting a Patch section and use the Pull Request Template.

Credits

Inspirated by my friend Gorka Laucirica's Hipchat v2 Api Client

Created by @benatespina - benatespina@gmail.com.
Copyright (c) 2014, 2015

Licensing Options

License