Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

A PHP-based client for the Reddit API

branch: master

This branch is 0 commits ahead and 0 commits behind master

Fetching latest commit…

Cannot retrieve the latest commit at this time

README.md

Introduction

This is a PHP Reddit API client.

As a quick taster, here's some sample code:

<?php

require_once 'RedditApiClient/Reddit.php';
use \RedditApiClient\Reddit;

$reddit = new Reddit;
$links  = $reddit->getLinksBySubreddit('botcirclejerk');

foreach ($links as $link) {
    echo $link->getTitle(), "\n";
}

And this is its output:

Wtf is up FUCK YOU BADGERS.
UPVOTE ME
So he fucking HATES me...not a very hard time communicating their thought process "well then have a boat, he can just use water. Take care of those people who walk in the room.
SOME MONDAY NIGHT, WE SHOULD REFUSE SHOWERS AND BATHS FOR MONTHS.
Blah blah stds blah blah blah blah blah stds blah blah blah blah.
CHICK WITH A DIAMOND SHOVEL, EARLY IN THY MORNING?
Argh damn freaking fracking fuck.
and thus begins scene 4: Tttttttttttttttt 5: ????? step 6: I live with daily floor washing robot
woopwoop pull ova  dat ass too fat
[citation needed] I was walking into my neighbor's cat pissed a bitch asshole. 
(╯°□°)╯︵ ┻━┻ (╯°□°)╯︵ ┻━┻ FREE KARMA.

For more details of how to use this library, see the Wiki

Installation

I haven't packaged this up in any way whatsoever yet. If you want to use it, just download the code and require_once Reddit.php

The code has all been written on a MacBook using PHP 5.3.6. It should work fine on other operating systems but I'm using namespaces so anything less than 5.3 is no good.

I've been burned too many times writing code that relied on the prescence of either cURL or pecl_http, only to find out that they weren't available and never would be in the production environment. This library has its own little implementations of HttpRequest and HttpResponse to save you from the usual "Hey this API client looks like it might work oh wait it depends on cURL never mind" bullshit.

Development Status

This library exposes the majority of the functionality of the API.

Anything to do with 'flair' isn't supported, but I can't see much demand for that functionality anyway, so I can't find the motivation to actully implement it.

Submitting self-posts isn't working yet, which is the only important missing feature left that I know of. I will remove this paragraph as soon as this is resolved.

Pull requests are welcome by the way!

Examples

Here's some code that uses the client to show the thread of comments on the top link in /r/programming

<?php

require_once 'Reddit/Reddit.php';
use \RedditApiClient\Reddit;


$reddit   = new Reddit;
$proggit  = $reddit->getLinksBySubreddit('programming');
$topLink  = $proggit[0];
$comments = $topLink->getComments();


echo $topLink->getTitle(), "\n\n";


foreach ($comments as $comment) {
    showComment($comment);
}


function showComment($comment, $level = 1)
{
    $prefix = str_pad('', $level * 4, ' ');

    $body = $comment->getBody();
    $body = wordwrap($body, 80 - strlen($prefix), "\n", true);
    $body = str_replace("\n", "\n{$prefix}", $body);

    echo $prefix, $body, "\n\n";

    $replies = $comment->getReplies();

    foreach ($replies as $reply) {
        showComment($reply, $level + 1);
    }
}

And here's the output:

We're developing a complete RPG in 14 days, live 
streaming 24 hours a day, to raise money for 
Child's Play. Watch me code it in real time!

    You should crosspost to /r/gamedev.

        Done!

            how about /r/gaming ?

                I put a xpost in /r/gaming.


[truncated]
Something went wrong with that request. Please try again.