Skip to main content
15 of 19
Updated for version 0.4 and the new 2.0 API changes.
Nathan Osman
  • 23.3k
  • 11
  • 61
  • 107

Stack.PHP - Completely Rewritten from the Ground Up [Now with limited support for v2.0 of the API!]

##API v2.0 Update I am pleased to announce that Stack.PHP has implemented some of the new changes introduced in version 2.0 of the API. You can now begin developing applications that use v2.0 of the API (note: you will need to be a part of the private beta to use Stack.PHP - you will need to provide Stack.PHP with an API key).

These changes are still in beta and things are not expected to work perfectly yet. The OAuth methods are not implemented yet and Stack.PHP does not yet make use of the backoff parameter. However, all non-authenticated methods are now supported, including the creation and use of filters. The test suite has also been updated to reflect the new methods and parameters.

Your feedback is appreciated at this time - I'm sure there are many bugs to be uncovered and the sooner they are found, the better.


Don't know where to start?

Stack.PHP is a PHP library for accessing and retrieving information from the Stack Exchange API.
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.
  • 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.

##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 release is 0.4 and can be found here:
http://launchpad.net/stackphp/0.4/0.4/+download/stackphp_source_0.4.zip

The previous stable version is available here:
http://launchpad.net/stackphp/0.3/stackphp0.3/+download/stackphp_0.3.tar.gz

Even older versions:
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.

##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