16

PHPstack

PHPstack is meant to be an easy to use PHP wrapper for the Stack Exchange API.

<?php
    require_once('lib/requestcore.class.php');
    require_once('phpstack.class.php');

    $so = new PHPstack('stackoverflow.com', 'yourAPIKey');
    $answer = $so->getAnswers(2921234));
    print_r($answer);        
?>

Returns the response from the SE object as a PHP object accessible from $answer->body, with additional details about the request available from $answer->header. This means you can easily check your rate limits - as they are available by default.

ResponseCore Object
(
    [header] => Array
        (
            [server] => nginx
            [date] => Thu, 27 May 2010 16:21:28 GMT
            [content-type] => application/json; charset=utf-8
            [connection] => close
            [cache-control] => private
            [content-encoding] => gzip
            [x-aspnetmvc-version] => 2.0
            [x-ratelimit-max] => 10000
            [x-ratelimit-current] => 9989
            [x-aspnet-version] => 2.0.50727
            [content-length] => 497
            [_info] => Array
                (
                    [url] => http://api.stackoverflow.com/0.8/answers/2921234?key=yourAPIKey
                    [content_type] => application/json; charset=utf-8
                    [http_code] => 200
                    [header_size] => 337
                    [request_size] => 359
                    [filetime] => -1
                    [ssl_verify_result] => 0
                    [redirect_count] => 0
                    [total_time] => 0.686
                    [namelookup_time] => 0
                    [connect_time] => 0.218
                    [pretransfer_time] => 0.218
                    [size_upload] => 0
                    [size_download] => 497
                    [speed_download] => 724
                    [speed_upload] => 0
                    [download_content_length] => 497
                    [upload_content_length] => -1
                    [starttransfer_time] => 0.686
                    [redirect_time] => 0
                    [method] => GET
                )

        )

    [body] => stdClass Object
        (
            [total] => 1
            [page] => 1
            [pagesize] => 30
            [answers] => Array
                (
                    [0] => stdClass Object
                        (
                            [answer_id] => 2921234
                            [accepted] => 
                            [answer_comments_url] => /answers/2921234/comments
                            [question_id] => 2921179
                            [owner] => stdClass Object
                                (
                                    [user_id] => 252591
                                    [user_type] => registered
                                    [display_name] => Crozin
                                    [reputation] => 2960
                                    [email_hash] => 06407bae4ac071e63df00ec6e34ed5ac
                                )

                            [creation_date] => 1274964794
                            [last_activity_date] => 1274964794
                            [up_vote_count] => 1
                            [down_vote_count] => 0
                            [view_count] => 12
                            [score] => 1
                            [community_owned] => 
                            [title] => How to get parameters from the URL to the model in cakePHP?
                        )

                )

        )

    [status] => 200
)

About

PHPstack is a wrapper for the StackExchange API. Currently it only supports making API requests through the provided methods but I intend to expand it to provide a variety of additional helper functions.

License

PHPstack is released under a BSD license.

Download

Download it from Github. (Download source is in the top right)

Platform

PHPstack should work on any host with PHP5 and CURL enabled.

Code

[RequestCore][3] is used for all the API requests, and I recommend that you check out the project it was developed for, CloudFusion, an awesome wrapper for AWS.

The code can be downloaded from Github, and I'll happily commit any improvements you make to the code - just fork it and send me a pull request.

Documentation

The code is kinda self-documenting, the method names describe exactly what's happening.

Roadmap

  • Pagination helpers
nobody
  • 864
  • 5
  • 19

2 Answers2

4

Update 26-06-2010

PHPstack now works with SE API v0.9 and includes functions to retrieve data from StackAuth.

Example of StackAuth sites function:

<?php
  //assume $so is our PHPstack class
  $sites = $so->sites();
  foreach($sites->body->api_sites as $site) {
    echo $site->name;
  }
?>
nobody
  • 864
  • 5
  • 19
1

It seem's you've updated some methods in the source since you wrote your code above (getAnswer is getAnswers) otherwise this works quite well so far.

  • 3
    ah yes, I thought I'd updated the example, I'll change that now. It makes more sense to me for them to be plural if you can get multiple lots of them. (If that makes any sense at all) – nobody May 28 '10 at 19:14