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!
I followed all your code, everything worked fine. Thank you.
Please help with getting user profile data in reddit.
Sure, are you trying to display your profile data or another users profile data from reddit?
Where to run the php script to obtain access token?
Where do I execute the php script to obtain access token?
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.
You can run it from the command line or in the browser and it will output the access token.
Thanks! Glad I could help out!
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
The error is saying the subreddit does not exist. Maybe check the spelling on the subreddit you are using.
mGpJNKlfoQVYjW
Helpful info. Fortunate me I found your site accidentally, and I’m stunned why this twist of fate
didn’t took place earlier! I bookmarked it.
I got this web site from my buddy who told me on the topic of this site and at the
moment this time I am browsing this web page and reading very informative posts at this time.
Pretty great post. I simply stumbled upon your blog
and wanted to say that I’ve really loved browsing your blog posts.
After all I will be subscribing to your rss feed and I am hoping you write once more very soon!
I got this web page from my friend who shared with me concerning this website and now this time I am visiting this website and reading very informative
posts at this time.
It is really a nice and useful piece of information. I’m satisfied that you simply shared this useful info with us.
Please stay us up to date like this. Thank you for sharing.
Usually I do not read article on blogs, but I would like to say that this write-up very forcedme to take a look at and do so! Your writing taste has been amazed meThanks, very nice article
Fantastic beat ! I wish to apprentice at the same
time as you amend your website, how could i subscribe for a blog website?
The account helped me a applicable deal. I have been a little bit acquainted of this your
broadcast provided vibrant transparent idea
I was recommended this website by my cousin. I’m not sure whether this post is written by him
as no one else know such detailed about my difficulty.
You’re wonderful! Thanks!
BrmRqbQtW
JcHAlUgzoODB
Thank youu for every other magnificent articⅼe. The plаcе eⅼse may just
anybody get that kind off information in such a perfect means
of writing? I’ve a presentation next week, aand I’m onn the sеarch for
such infоrmation.
Here is mmʏ webite – best inmate text service
Oh my goodness! Impressive article dude! Thanks, However I am having difficulties with your RSS.
I don’t understand the reason why I am unable
to join it. Is there anybody getting the same RSS issues?
Anyone that knows the answer can you kindly
respond? Thanks!!