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.
- Login to reddit and go to https://reddit.com/prefs/apps.
- 2Under “developer applications” click “create app”.
- 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.
- 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.
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.
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!
We have successfully posted to Reddit with PHP using their API! Here are some helpful links relating to this blog post.
That is going to do it for this post! Leave any comments/questions/concerns below and thanks for stopping by the blog!