Reddit API with PHP



In this post we are going to connect to the Reddit API using PHP. We will create a Reddit App, get an access token, and post to a subreddit with a PHP script using the Reddit API.

Step 1: Create a Reddit App

Before we can start coding, we need to create a Reddit App. No Reddit App, no API access. In order to connect to the Reddit API you need a client id, client secret, username, and password. To get these things you need a Reddit app.

  1. Login to reddit and go to https://reddit.com/prefs/apps.
  2. 2Under “developer applications” click “create app”.
  3. Fill out the app info like the image below and click “create app”. Make sure to select “script” as well. You don’t need to enter an “about url” but a “redirect url” is required. You can enter anything for the “redirect url” as we will not be using it at all.Create Reddit App
  4. Once your app is created you should see it listed under “developer applications” like the image below. From here we can get our client id, client secret, username, and password, for connecting to the app.Reddit App

 

Step 2: Obtain an Access Token

To make calls to the Reddit API we need an access token. That access token will then be passed along with each API call we make. Without the access token, any API call we try to make will fail. The code below will connect to the access_token endpoint on Reddit and give us an access token if we pass along the right parameters. We need to pass along our username, password, client id, and client secret. The client id and client secret come from the Reddit App created in the first step.

// reddit username
$username = 'YOUR-REDDIT-USERNAME';

// reddit password
$password = 'YOUR-REDDIT-PASSWORD';

// client id
$clientId = 'YOUR-REDDIT-APP-CLIENT-ID';

// client secret
$clientSecret = 'YOUR-REDDIT-APP-CLIENT-SECRET';

// post params 
$params = array(
    'grant_type' => 'password',
    'username' => $username,
    'password' => $password
);

// curl settings and call to reddit
$ch = curl_init( 'https://www.reddit.com/api/v1/access_token' );
curl_setopt( $ch, CURLOPT_USERPWD, $clientId . ':' . $clientSecret );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'POST' );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $params );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );

// curl response from reddit
$response_raw = curl_exec( $ch );
$response = json_decode( $response_raw );
curl_close($ch);

// display response from reddit
var_dump( $response );

The “var_dump( $response )” in the code above should give you an array of data back from Reddit including an access token and token type. Your response should look like the image below.

Reddit Access Token

 

Step 3: Post To A Subreddit

To post to a subreddit through the API the subreddit has to have the correct permissions for your user. For testing purposes I would suggest you just create a new subreddit at https://www.reddit.com/subreddits/create and use it for your own testing with the API. Note you do need to have some karma before you can create a subreddit.

The code snippet below will post a link to a subreddit. Step 2 gave us our access token and token type. Those need to be filled in in the code below in order for the call to go through. The subreddit post data also needs to be filled in with the post title, post url, and subreddit name, subreddit display name, and username.

// access token
$accessToken = 'ACCESS-TOKEN';

// access token type
$accessTokenType = 'ACCESS-TOKEN-TYPE';

// reddit username
$username = 'YOUR-REDDIT-USERNAME';

// subreddit name (no spaces)
$subredditName = 'SUBREDDIT-NAME';

// subreddit display name (can have spaces)
$subredditDisplayName = 'SUBREDDIT-DISPLAY-NAME';

// subreddit post title
$subredditPostTitle = 'SUBREDDIT-POST-TITLE';

//subreddit post url
$subredditUrl = 'SUBREDDIT-POST-URL';

// api call endpoint
$apiCallEndpoint = 'https://oauth.reddit.com/api/submit';

// post data: posting a link to a subreddit
$postData = array(
    'url' => $subredditUrl,
    'title' => $subredditPostTitle,
    'sr' => $subredditName,
    'kind' => 'link'
);

// curl settings and call to post to the subreddit
$ch = curl_init( $apiCallEndpoint );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_USERAGENT, $subredditDisplayName . ' by /u/' . $username . ' (Phapper 1.0)' );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array( "Authorization: " . $accessTokenType . " " . $accessToken ) );
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'POST' );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $postData );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );

// curl response from our post call
$response_raw = curl_exec( $ch );
$response = json_decode( $response_raw );
curl_close( $ch );

After running the post to reddit script above, check the subreddit  and make sure the post worked! Here is a screenshot of my successful Reddit post!

Reddit Post

We have successfully posted to Reddit with PHP using their API! Here are some helpful links relating to this blog post.

YouTube Video

Code on GitHub

Reddit API Documentation

That is going to do it for this post! Leave any comments/questions/concerns below and thanks for stopping by the blog!

9 comments

  1. Awesome! Super simple to follow. With a few modifications it was just what I need to start posting interviews from my site to our new subreddit.

    Thanks a lot for the guide.

  2. Every Thing Look Ok
    But when i run scripts post to reddit code i got some error

    error.SUBREDDIT_NOEXIST.field-sr
    … that subreddit doesn’t exist

    how i fix it
    Thank you

    1. The error is saying the subreddit does not exist. Maybe check the spelling on the subreddit you are using.

Leave a Reply to bax237 Cancel reply

Your email address will not be published. Required fields are marked *