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.
Hello, after reading this amazing piece of writing iam also delighted to share my familiarity here with friends.Feel free to surf to my blog … car repair woodbridge
The need for versatility adjustment should be done to Rosetta Clarance Nealson
I am so grateful for your blog post.Much thanks again. Really Great.
Really informative blog.Really thank you! Keep writing.
Thanks , I’ve recently been searching for info about this topic for ages and yours is the best I have discovered so far. But, what about the bottom line? Are you sure about the source?
Great, thanks for sharing this blog.Much thanks again. Fantastic.
Thanks so much for the blog article.Much thanks again. Awesome.
Aw, this was an exceptionally nice post. Spending some time and actual effort to make a superb article… but what can I say… I put things off a lot and never manage to get anything done.
Im grateful for the blog post.Really thank you! Awesome.
stromectol oral – generic ivermectin for humans stromectol australia
This is a topic that is near to my heart Take care! Exactly where are your contact details though?
Whoah this blog is magnificent i really like reading your articles. Keep up the good paintings! You realize, a lot of persons are searching round for this information, you could aid them greatly.
An interesting discussion is definitely worth comment. I believe that you need to write more on this subject matter, it may not be a taboo subject but generally people don’t talk about such topics. To the next! Cheers!!
Appreciate you sharing, great blog article.Really thank you! Really Great.
Hey, thanks for the article post.Much thanks again. Really Great.
Very neat blog.Really looking forward to read more. Much obliged.
Awesome blog post.Thanks Again. Fantastic.
A motivating discussion is definitely worth comment. I think that you should publish more on this issue, it might not be a taboo matter but usually people don’t discuss these issues. To the next! Kind regards!!
I loved your article.Really looking forward to read more.
Article writing is also a excitement, if you be acquaintedwith after that you can write or else it is complicated to write.
I love what you guys are up too. Such clever work and coverage!Keep up the amazing works guys I’ve added you guys to our blogroll.
Really enjoyed this article post.Much thanks again. Cool.
Hi there, all is going well heгe and ofcourse every one issharіng facts, that’s in fact excellent, keep upԝriting.my blog post – situs Judi online terbaik
Thx so much ! Appreciate the useful information. I love sucking dick btw hmu
There’s definately a great deal to learn about this subject. I love all of the points you made.
Hi there friends, fastidious article and goodarguments commented at this place, I am in fact enjoying by these.
Die glänzende Lackierung war Gegenstand verschiedener Studien
hydroxychloroquine 300 hydroxychloroquine over the counter – plaquenil hydroxychloroquine
your situation; many of us have developed some nice practices and we are looking to trade
I enjoy looking through an article that will make people think. Also, thank you for allowing for me to comment.
I like the valuable info you supply for your articles.I’ll bookmark your blog and take a look at once more hereregularly. I’m relatively certain I will be told plenty ofnew stuff proper here! Good luck for the following!
Superb post.Never knew this, thanks for letting me know.
I love what you guys are usually up too. This kind of clever work and exposure!Keep up the good works guys I’ve incorporated you guys to my own blogroll.
Hey there are using WordPress for your blog platform? I’m new to the blog worldbut I’m trying to get started and create my own. Do you require any coding expertiseto make your own blog? Any help would be really appreciated!
This is a great tip particularly to those new to the blogosphere. Simple but very accurate informationÖ Thanks for sharing this one. A must read post!
Thank you ever so for you blog.Much thanks again. Cool.Loading…
Thanks a lot for the article.Thanks Again. Want more.
I like what you guys tend to be up too. Such clever work and coverage!Keep up the superb works guys I’ve incorporatedyou guys to my blogroll.
Major thankies for the post. Much obliged.
Your mode of explaining everything in this article is genuinely good, all be capable of simply be aware of it, Thanksa lot.
Im grateful for the blog post.Really thank you! Cool.
Merely wanna admit that this is invaluable , Thanks for taking your time to write this.
Excellent blog you’ve got here.. Itís hard to find good quality writing like yours nowadays. I truly appreciate individuals like you! Take care!!
These are truly enormous ideas in about blogging. You have touched some fastidious factors here. Any way keep up wrinting.
Amazing. Occur to be so good of which you’re so good at this point. Your help woke myself up. Thank you extremely much.
Rattling nice style and design and excellent content, hardly anything else we want :D.Here is my blog :: Mountain Pure CBD Gummies Review
That is a really good tip especially to those new to the blogosphere. Short but very precise information… Thank you for sharing this one. A must read article.
Really appreciate you sharing this article.Really looking forward to read more. Want more.
online pharmacy india canadian pharmacies canadian pharmacy 24h
It’s in reality a great and helpful piece of information. I am satisfied that you just shared this helpful information with us. Please stay us informed like this. Thanks for sharing.
Great article.Really thank you! Really Great.
Great, thanks for sharing this article.Much thanks again. Cool.
Muchos Gracias for your post.Really looking forward to read more. Want more.
Great, thanks for sharing this post.Really thank you!
Wow, great post.Really looking forward to read more. Keep writing.
academic writing uk – essaymbp.com my friend essay writing
I loved your blog article.Thanks Again. Cool.
Great blog article.Thanks Again. Keep writing.
flccc doctors who prescribe ivermectin wsj ivermectin
I like the helpful information you provide in your articles. I will bookmark your blog and check again here regularly. I am quite sure I will learn many new stuff right here! Best of luck for the next!
I cannot thank you enough for the blog post. Great.
Aw, this was an exceptionally good post. Taking a few minutes and actual effort tomake a superb article… but what can I say… I put things off a lot and don’t manage to get anything done.
I value the post.Much thanks again. Really Cool.
I wass able to fiind good innfo from your blog posts.
It as hard to come by well-informed people for this topic, however, you sound like you know what you are talking about! Thanks
I really enjoy the article.Really thank you! Fantastic.
I cannot thank you enough for the post.Thanks Again.
Generally I do not read article on blogs, but I would liketo say that this write-up very forced me to try and do so!Your writing style has been amazed me. Thanks, quite nice article.
Great article. I will be going through many of these issues as well..Review my blog post … moisturize your skin
I truly appreciate this article.Really thank you! Fantastic.
Very neat post.Thanks Again. Really Great.
Thanks again for the blog post.Thanks Again. Will read on…
skambucius,sievietem.seksikad iepazisanas seksam
Fastidious answer back in return of this question with solid arguments and explaining all on the topic of that.
Very informative blog article.Really looking forward to read more. Really Great.
Thanks so much for the blog. Cool.Loading…
Xoilac Tv Trực Tiếp đá Bóng 12betĐội tuyển nước Việt Nam chỉ cần thiết một kết quả hòa có bàn thắng nhằm lần loại hai góp mặt tại World Cup futsal. Nhưng, để làm được điều này
F*ckin’ awesome issues here. I’m very satisfied to peer your post. Thanks a lot and i am looking forward to contact you. Will you kindly drop me a mail?
Thank you ever so for you blog post.Really thank you! Much obliged.
Very good blog.Thanks Again. Will read on…
Your style is unique compared to other people I have read stuff from.Many thanks for posting when you have the opportunity, Guess I will just bookmark this blog.
how to lose weight while taking cymbalta can taking duloxetine increase blood flow to penis
Hi mates, its impressive post regarding tutoringand fully defined, keep it up allthe time.
I do trust all of the concepts you’ve introduced to your post. They’re really convincing and can definitely work. Still, the posts are very quick for novices. May you please lengthen them a little from subsequent time? Thanks for the post.
hello!,I love your writing very a lot! percentage we keerp upa correspondence more approximately your article on AOL?I need an expert on this space to unravel my problem. Maybe that’s you!Haviing a look forward to see you.
Generally I don’t learn article on blogs, but I wish to say thatthis write-up very forced me to take a look at and do it!Your writing style has been amazed me. Thanks,quite great post.
I appreciate you sharing this blog article.Really thank you! Great.
Im grateful for the article post.Thanks Again. Keep writing.
Thanks-a-mundo for the blog article.Thanks Again. Fantastic.