PHP code example of snlbaral / reddit-php

1. Go to this page and download the library: Download snlbaral/reddit-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

snlbaral / reddit-php example snippets



use Snlbaral\Reddit\Reddit;

$reddit = new Reddit();

// getPosts() @params $subreddit_name, $token (optional), $dist (optional), $sort (optional)
$posts = $reddit->getPosts('subreddit_name');
// returns array of posts, next page token, dist, sorting method, subreddit name
// print_r($posts);

// To Get next page/thread posts use following:
$next_page = $reddit->getPosts('subreddit_name', $posts['token']);
// returns array of next page/thread posts, next page token, dist, sorting method, subreddit name

$subreddit_info = $reddit->getInfo('subreddit_name');

$postId = 'postId or token';
$post = $reddit->viewPost($postId);

// userOverview() @params $username, $token (optional), $dist (optional), $sort (optional)
$overview = $reddit->userOverview('username');
// returns array of user's posts, comments, next page token, dist, sorting method, username

// To Get next page/thread posts and comments of user, use following
$next_page_overview = $reddit->userOverview('username', $overview['token']);

// userPosts() @params $username, $token (optional), $dist (optional), $sort (optional)
$user_posts = $reddit->userPosts('username');
// returns array of user's posts next page token, dist, sorting method, username

// To Get next page/thread posts of user, use following
$next_page_user_posts = $reddit->userPosts('username', $user_posts['token']);

// userComments() @params $username, $token (optional), $dist (optional), $sort (optional)
$comments = $reddit->userComments('username');
// returns array of user's comments, next page token, dist, sorting method, username

// To Get next page/thread comments of user, use following
$next_page_comments = $reddit->userComments('username', $comments['token']);

// downloadMediasBySub() @params $subreddit_name, $token (optional), $dist (optional), $sort (optional), $dir (optional)
$downloads = $reddit->downloadMediasBySub('subreddit_name');
// Downloads All Media Files from first page of subreddit using async, saves in $dir location
// returns array of next page token, dist, sorting method and subreddit name

// To download next page/thread media files, use following
$next_page_downloads = $reddit->downloadMediasBySub('subreddit_name', $downloads['token']);
// Downloads All Media Files from next page of subreddit using async, saves in $dir location
// returns array of next page token, dist, sorting method and subreddit name

parseMediaByPosts($posts); //$posts array of posts from getPosts()
downloadMediasByPosts($posts); //$posts array of posts from getPosts()
parseMedia($media); //each $posts has media loop $posts and pass $post['media'];

try {
	$downloads = $reddit->downloadMediasBySub('subreddit_name', false, 25, 'new', 'mydownloads');
	print_r($downloads);
	//echo $downloads['token'];
} catch (Exception $e) {

	if($e->getResponse()) {
		$response = $e->getResponse();
		$responseBodyAsString = $response->getBody()->getContents();
		var_dump($responseBodyAsString);
	} else {
		var_dump($e);
	}

}