PHP code example of waffelheld / phpredditapi

1. Go to this page and download the library: Download waffelheld/phpredditapi 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/ */

    

waffelheld / phpredditapi example snippets



it = new \RedditApi\Reddit();

$state = "RANDOM_UNIQUE_STRING";
$redirectUrl = "YOUR_RETURN_URL";
$clientId = "CLIENT_ID";
    $secret = "CLIENT_SECRET";

if(!isset($_GET['code']) && !isset($_GET['state'])) {
    $endpoint = "authorize";
    $params = array(
        'client_id'         => $clientId,
        'response_type'    => 'code',
        'redirect_uri'      => $redirectUrl,
        'duration'          => 'permanent',
        'state'             => $state,
        'scope'             => 'identity,edit,flair,history,modconfig,modflair,modlog,modposts,modwiki,mysubreddits,privatemessages,read,report,save,submit,subscribe,vote,wikiedit,wikiread'
    );
    
    $url = $reddit->getBaseUrl();
    $url .= $endpoint.'?'.http_build_query($params);
    header('Location: '.$url);
    
} else {
    
    
    if($_GET['state'] !== $state){
        echo 'state not matching';
        exit;
    }
    $params = array(
        'grant_type'    => 'authorization_code',
        'code'          => $_GET['code'],
        'redirect_uri'  => $redirectUrl
    );
    
    $reddit->setCredentials($clientId, $secret);
    
    $result = $reddit->auth($params);
    //access token in $result['access_token'];
}


$reddit = new \RedditApi\Reddit('YOUR_TOKEN');
$result = $reddit->getAuth('api/me');

$reddit = new \RedditApi\Reddit('YOUR_TOKEN');
$params = array(
    //declare params
);
$result = $reddit->getAuth('api/me', $params);