PHP code example of usman / reddit-php-api

1. Go to this page and download the library: Download usman/reddit-php-api 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/ */

    

usman / reddit-php-api example snippets

 


/**
 * This demonstrates how to authenticate with Reddit and send api requests
 */

/*
 * First you need to make sure you've used composers auto load. You have is probably 
 * already done this before. You usually don't bother..
 */
//

    exit();
} elseif ($reddit->hasError()) {
    echo "User canceled the login.";
    exit();
}

//if not authenticated
$url = $reddit->getLoginUrl();
echo "<a href='$url'>Login with Reddit</a>";


$reddit=new Usman\Reddit\Reddit('app_id', 'app_secret');
$reddit->setAccessToken('access_token_from_db');

$options = array('json'=>
    array(
        'comment' => 'Im testing Usman Reddit Api! https://github.com/usman/Reddit-API',
        'visibility' => array(
            'code' => 'anyone'
        )
    )
);

$result = $reddit->post('v2/people/~/shares', $options);

var_dump($result);

// Prints: 
// array (size=2)
//   'updateKey' => string 'UPDATE-01234567-0123456789012345678' (length=35)
//   'updateUrl' => string 'https://www.Reddit.com/updates?discuss=&scope=01234567&stype=M&topic=0123456789012345678&type=U&a=mVKU' (length=104)


$options = array(
'format' => 'xml',
'body' => '<share>
 <comment>Im testing Usman Reddit client! https://github.com/usmanmalik/Reddit-API</comment>
 <visibility>
   <code>anyone</code>
 </visibility>
</share>');

$body = array(
    'comment' => 'Im testing Usman Reddit client! https://github.com/usmanmalik/Reddit-API-client',
    'visibility' => array('code' => 'anyone')
);

$reddit->post('v2/people/~/shares', array('json'=>$body));
$reddit->post('v2/people/~/shares', array('body'=>json_encode($body)));

// By constructor argument
$reddit=new Usman\Reddit\Reddit('app_id', 'app_secret', 'xml');

// By setter
$reddit->setFormat('xml');

// Set format for just one request
$reddit->post('v2/people/~/shares', array('format'=>'xml', 'body'=>$body));

// By constructor argument
$reddit=new Usman\Reddit\Reddit('app_id', 'app_secret');


$reddit->get('v2/userinfo');


$reddit=new Usman\Reddit\Reddit('app_id', 'app_secret');
$reddit->setStorage(new IlluminateSessionStorage());

$scope = 'email,openid,profile,w_member_social';
//or 
$scope = array('email', 'openid', 'profile', 'w_member_social');

$url = $reddit->getLoginUrl(array('scope'=>$scope));
echo "<a href='$url'>Login with Reddit</a>";
bash
composer 
bash
composer