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!
Aw, this was a very goоd post. Sρending some time and actual effort to
maқe a very good article… but what can I say… I
put things off а whole lot and don’t manage to get anything done.
Feel free to visit my web site best inmate text service
Wow, amɑᴢing Ƅⅼog layout! How lengthy haѵe yoս ever been running a blog fоr?
yoս mаke running a blog glance easy. The overall glance of
your web sіte іѕ magnificent, as neatly as the content materiаl!
my web blog; inmate texting
Everyting is very open wirh a vewry clear clarification of thhe issues.
It waas really informative. Youur ebsite iss extremely helpful.
Thanks foor sharing!
I appreciate you sharing this blog.Much thanks again. Really Cool.
Why visitorss still uuse to readd news papers when iin thiss technological world alll is existing oon net?
biaxin for sale chloramphenicol capsulescipro for sale
I appreciate you sharing this blog article.Thanks Again.
Very informative post. Will read on…
Im thankful for the article post.Thanks Again.
Very informative article. Really Cool.
wow, awesome blog.Really looking forward to read more. Cool.
Major thanks for the blog post.Really thank you! Awesome.
Pretty! This was a really wonderful article.Thank you for supplying this info.
It’s impressive that you are getting thoughts from this articleas well as from our dialogfue made here.
excellent points altogether, you just won a logo new reader.What may you recommend about your submit that you made afew days ago? Any certain?Review my blog – tillernatural.com
Thanks again for the article.Really thank you! Will read on…
how to get ivermectin ivermectin pour on for sheep
ivermectin oral 0 8 ivermectin – stromectol
Some genuinely great info , Gladiola I detected this.
side effects of atorvastatin calcium side effects of lipitor and alcohol
Heya just wantdd to give yyou a brief heads up and let you knowa few oof the pictures aren’t loading correctly.I’m not sure why but I thjnk its a linking issue.I’ve trtied it in two different internet browsers and both show the sameoutcome.
ovtgqxtwj qfbpq nprklyc yyhb tiywpfmyrilqfcf
Hey there! I’m at work surfing around your blog from my new iphone 4!Just wanted to say I love reading your blog and look forward toall your posts! Keep up the fantastic work!
I’d need to verify with you here. Which is not one thing I normally do! I enjoy studying a submit that can make individuals think. Also, thanks for allowing me to comment!
Good blog you have got here.. Itís difficult to find high-quality writing like yours these days. I honestly appreciate individuals like you! Take care!!
Hola! I’ve been following your blog for a while now and
finally got the bravery to go ahead and give you a shout out from Atascocita Tx!
Just wanted to tell you keep up the fantastic job!
Hi there, its good piece of writing on the topic of media print, we all know mediais a wonderful source of information.
You made some good points there. I looked on the internet for the subject and found most individuals will approve with your website.
Hi there, just turned into aware of your weblog via Google, and located that it is truly informative. I am gonna be careful for brussels. I will be grateful in the event you proceed this in future. A lot of people can be benefited from your writing. Cheers!
Hi my loved one! I want to say that this article is awesome, great written and come with approximately all vital infos. I’d like to look extra posts like this .
Very well written article. It will be beneficial to anybody who usess it, including me. Keep doing what you are doing – i will definitely read more posts.
I liked as much as you will receive performed right here. The comic strip is tasteful, your authored material stylish. nonetheless, you command get got an nervousness over that you want be turning in the following. sick definitely come more until now once more as exactly the same just about a lot often inside case you shield this hike.
Hello there, You’ve done an incredible job. I will definitely digg it and personally suggest to my friends. I’m sure they will be benefited from this site.
Hello, i think that i saw you visited my blog thus i came to “return the favor”.I’m trying to find things to enhance my site!I suppose its ok to use a few of your ideas!!
What’s Happening i am new to this, I stumbled upon this I have found It absolutely helpful and it has helped me out loads. I hope to contribute & help other users like its helped me. Good job.
magnificent points altogether, you simply gained a new reader. What would you suggest about your post that you made a few days ago? Any positive?
You actually mentioned that fantastically! canada pharmacies online
It’s a pity you don’t have a donate button! I’d certainly donate to this fantastic blog! I guess for now i’ll settle for bookmarking and adding your RSS feed to my Google account. I look forward to fresh updates and will share this site with my Facebook group. Talk soon!
Hi! Do you know if they make any plugins to assist with Search Engine Optimization? I’m trying to get my blog to rank for some targeted keywords but I’m not seeing very good gains. If you know of any please share. Appreciate it!
At this time it looks like BlogEngine is the best bloggingplatform out there right now. (from what I’ve read) Is that whatyou’re using on your blog?
This is really attention-grabbing, You are an excessively skilled blogger. I have joined your rss feed and look forward to in search of more of your excellent post. Additionally, I’ve shared your site in my social networks!
These are genuinely fantastic ideas in concerning blogging. You have touched some pleasant factors here. Any way keep up wrinting.
Very good post.Really thank you! Want more.