jord-jd/php-reddit-search

PHP Reddit Search

Maintainers

Package info

github.com/Jord-JD/php-reddit-search

pkg:composer/jord-jd/php-reddit-search

Transparency log

Fund package maintenance!

DivineOmega

Statistics

Installs: 7

Dependents: 0

Suggesters: 0

Stars: 1

Open Issues: 0

v2.0.1 2026-07-18 04:49 UTC

This package is auto-updated.

Last update: 2026-07-18 04:49:50 UTC


README

Search Reddit posts through Reddit's OAuth API and receive normalized search result objects.

Installation

composer require jord-jd/php-reddit-search

Authentication

Reddit requires API clients to authenticate with OAuth and send a unique, descriptive user agent. Create an application in Reddit's app preferences, request a token with the read scope, and follow Reddit's OAuth documentation. Tokens expire, so refresh the token in your application before constructing a new searcher when necessary.

Usage

use JordJD\RedditSearch\RedditSearcher;

$searcher = new RedditSearcher(
    $accessToken,
    'php:your-application:v1.0 (by /u/your_reddit_username)'
);

$results = $searcher->search('PHP programming language', [
    'subreddit' => 'PHP',
    'sort' => 'top',
    'time' => 'year',
    'limit' => 25,
    'include_over_18' => false,
]);

foreach ($results as $result) {
    echo $result->getTitle();
    echo $result->getDescription();
    echo $result->getUrl();
    echo $result->getPermalink();
    echo $result->getSubreddit();
    echo $result->getAuthor();
    echo $result->getScore();
    echo json_encode($result);
}

Supported options are after, include_over_18, limit (1 to 100), sort (relevance, hot, top, new, or comments), subreddit, and time (hour, day, week, month, year, or all). Invalid options fail before an HTTP request is made.

The normalized score ranges from 1.0 for the first result down toward zero. Use getLastRateLimit() after a built-in HTTP request to inspect Reddit's used, remaining, and reset rate-limit values. Use getAfter() to obtain the next-page cursor and pass it back as the after option.

Custom HTTP transport

The optional third constructor argument is a callable, useful for applications with an existing HTTP stack. It receives the URL, header array, and timeout, and must return the response body as a string.

$searcher = new RedditSearcher(
    $accessToken,
    $userAgent,
    function (string $url, array $headers, int $timeout): string {
        return $yourHttpClient->get($url, $headers, $timeout);
    },
    10
);

Version 2 supports PHP 7.1 through current PHP 8.x releases. It replaces the unauthenticated public endpoint used by version 1, which no longer complies with Reddit's API rules.