benatespina/stack-exchange-api-client

StackExchange v2.2 API client

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

This package is not auto-updated.

Last update: 2024-11-09 16:32:25 UTC


README

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

Requirements

PHP >= 7.1

Installation

The easiest way to install this bundle is using Composer

$ 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 it so, this library allows to instantiate StackExchange client with or without Authentication. Furthermore there two ways to use this library.

Default usage

<?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!');

Full customized usage

<?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!');

Current status

The API has many methods, so the calls' implementation status are separated by type in the following files:

Tests

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

Run the following command to launch tests:

$ vendor/bin/phpspec run -fpretty

Contributing

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.

Credits

This library is created by:

@benatespina - benatespina@gmail.com

Licensing Options

License