Skip to main content
19 of 19
This library has its own tag, so this Q should have it too, for easier search.

Stack.PHP - Powerful and Extensive Library for Accessing the Stack Exchange API - Including Full Support for v2.0.

Don't know where to start?

Begin by checking out the Stack.PHP website.
Then head over here for a quick tutorial on setting up and using Stack.PHP.

##About

Why you should use Stack.PHP:

Stack.PHP has been completely rewritten from scratch to facilitate a comprehensive restructuring of the code. These new changes allow certain tasks to be coded in a much more logical manner while still providing full access to all routes in the API.

Here are just some of the features Stack.PHP offers:

  • A flexible caching system. Stack.PHP is designed to use as few API requests as possible. The library comes with two classes that can be set to automatically cache the responses of all API requests made: an SQL cache class (compatible with all database systems that have a PDO driver) and a filestore class (which stores the cached data on the filesystem). If none of the classes meets your needs, you can easily implement the CacheBase class in one of your own classes and customize it to your heart's content.
  • An intuitive pagination class. All requests (with perhaps one or two exceptions) return an instance of a Response or PagedResponse object. This object provides an easy means of stepping through the objects returned. In the case of a PagedRequest object, additional pages of data are automatically fetched as needed as you step through the data returned. This code that uses a Response and PagedResponse object is basically identical.
  • Fully tested. The archives available for download and the code on Launchpad come with a tests folder. This folder contains an exhaustive test suite for Stack.PHP that runs through every API route, comparing the responses against expected output. These tests also make sure the utility code (such as the cache management code) is functioning as expected.
  • Fully documented. The entire codebase makes use of Doxygen comments throughout, making it easy to quickly generate HTML pages that describe in detail what each of the methods, enumerations, etc. do and any potential side effects you might encounter. The documentation can be found in the doc folder in the archives.
  • Easy to pick up and learn. To commemorate the first release in the 0.4 series, I have written a 7 page document on Google Docs which guides you through the basics of using the library.
  • Complete support for authentication. Stack.PHP supports the API's OAuth 2.0 methods out-of-the-box and without any third-party libraries. Everything just works - and you can immediately get a feel for how authentication works by dropping an API key / ID into one of the examples and trying it out. Both the implicit and explicit authentication flows are supported.
  • An organized exception class. Exceptions can come from all over the place in Stack.PHP (from cURL, from the cache classes, etc.), so it made sense to create a flexible exception class that captured the relevant information. You can see how this works by examining the test suite that is included with the library.
  • OutputHelper class to save you from writing HTML / CSS / JS. When designing a web application that uses Stack.PHP, you will find yourself writing certain snippets of code again and again. Stack.PHP saves you from having to do this by offering a simple OutputHelper class that generates HTML / CSS / JS code for you. Do you want to generate a <select> containing all Stack Exchange sites without their corresponding Metas? No problem - this can be solved with one line of code.
  • A full slate of real-world examples. It's one thing to know how to use Stack.PHP in theory. It's another thing to understand what typical and proper usage of the library looks like. That's why the library ships with a number of examples that demonstrate best practices and usage patterns - examples that range from really simple to very complex.

##Code Snippet

Simple example demonstrating usage:

<?php

// Imports all of the files we need
require_once 'path_to_stackphp/api.php';

// Create a Site object for Stack Apps and get the questions tagged 'app'
$stackapps = API::Site('stackapps');
$apps      = $stackapps->Questions()->Tagged('app')->Exec();

// Notice that we are fetching ALL apps across ALL pages here
while($app = $apps->Fetch())
    echo "{$app['title']}\n";

?>

##License

How can I use Stack.PHP?

Stack.PHP is available under the MIT License - you are free to make any modifications to the code and use the library in a commercial application.

##Download

Where can I get Stack.PHP?

The latest beta release is 0.5 beta 1 and can be downloaded here:
http://launchpad.net/stackphp/0.5/0.5beta1/+download/stackphp-0.5beta1-source.tar.gz

The previous stable release can be found here:
http://launchpad.net/stackphp/0.4/0.4/+download/stackphp_source_0.4.zip

Even older versions:
http://launchpad.net/stackphp/0.3/stackphp0.3/+download/stackphp_0.3.tar.gz
http://launchpad.net/stackphp/0.2/stackphp0.2/+download/stackphp_0.2.tar.gz
http://launchpad.net/stackphp/0.1/stackphp0.1/+download/stackphp_0.1.tar.gz

##Platform

What do I need to run Stack.PHP?

PHP 5.2 or higher is required for most of the library. The SQLite driver for the cache class has a bug that prevents it from working in PHP 5.2, however.

##Contact

Who do I talk to if I have a question?

I can be reached at [email protected].

##Code

Gimme teh codez!!11!

The code can be retrieved from http://launchpad.net/stackphp

Contributions to stack.PHP are always welcome.
Contact me at the above address or on LaunchPad if you are interested.

Nathan Osman
  • 23.3k
  • 11
  • 61
  • 107