{"id":56,"date":"2019-01-13T20:50:04","date_gmt":"2019-01-13T20:50:04","guid":{"rendered":"https:\/\/justinstolpe.com\/blog\/?p=56"},"modified":"2019-01-18T00:02:23","modified_gmt":"2019-01-18T00:02:23","slug":"reddit-api-with-php","status":"publish","type":"post","link":"https:\/\/justinstolpe.com\/blog\/2019\/01\/13\/reddit-api-with-php\/","title":{"rendered":"Reddit API with PHP"},"content":{"rendered":"<p><center><br \/>\n<iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/1Prmvgu-Wlo\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen=\"allowfullscreen\"><\/iframe><br \/>\n<\/center><\/p>\n<p>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.<\/p>\n<h3 style=\"margin-top: 10px;\"><strong>Step 1: Create a Reddit App<\/strong><\/h3>\n<p>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.<\/p>\n<ol>\n<li>Login to reddit and go to https:\/\/reddit.com\/prefs\/apps.<\/li>\n<li>2Under &#8220;developer applications&#8221; click &#8220;create app&#8221;.<\/li>\n<li>Fill out the app info like the image below and click &#8220;create app&#8221;. Make sure to select &#8220;script&#8221; as well. You don&#8217;t need to enter an &#8220;about url&#8221; but a &#8220;redirect url&#8221; is required. You can enter anything for the &#8220;redirect url&#8221; as we will not be using it at all.<a href=\"https:\/\/justinstolpe.com\/blog\/wp-content\/uploads\/2019\/01\/redditCreateApp.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-59\" style=\"border: 1px solid #333; margin-top: 10px; margin-bottom: 10px;\" src=\"https:\/\/justinstolpe.com\/blog\/wp-content\/uploads\/2019\/01\/redditCreateApp.png\" alt=\"Create Reddit App\" width=\"926\" height=\"343\" srcset=\"https:\/\/justinstolpe.com\/blog\/wp-content\/uploads\/2019\/01\/redditCreateApp.png 926w, https:\/\/justinstolpe.com\/blog\/wp-content\/uploads\/2019\/01\/redditCreateApp-300x111.png 300w, https:\/\/justinstolpe.com\/blog\/wp-content\/uploads\/2019\/01\/redditCreateApp-768x284.png 768w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/a><\/li>\n<li>Once your app is created you should see it listed under &#8220;developer applications&#8221; like the image below. From here we can get our client id, client secret, username, and password, for connecting to the app.<a href=\"https:\/\/justinstolpe.com\/blog\/wp-content\/uploads\/2019\/01\/testappedited.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-65\" style=\"border: 1px solid #333; margin-top: 10px; margin-bottom: 10px;\" src=\"https:\/\/justinstolpe.com\/blog\/wp-content\/uploads\/2019\/01\/testappedited.png\" alt=\"Reddit App\" width=\"904\" height=\"388\" srcset=\"https:\/\/justinstolpe.com\/blog\/wp-content\/uploads\/2019\/01\/testappedited.png 904w, https:\/\/justinstolpe.com\/blog\/wp-content\/uploads\/2019\/01\/testappedited-300x129.png 300w, https:\/\/justinstolpe.com\/blog\/wp-content\/uploads\/2019\/01\/testappedited-768x330.png 768w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h3><strong>Step 2: Obtain an Access Token<br \/>\n<\/strong><\/h3>\n<p>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.<\/p>\n<pre class=\"height:800 lang:php decode:true\" title=\"Get Reddit Access Token\">\/\/ reddit username\r\n$username = 'YOUR-REDDIT-USERNAME';\r\n\r\n\/\/ reddit password\r\n$password = 'YOUR-REDDIT-PASSWORD';\r\n\r\n\/\/ client id\r\n$clientId = 'YOUR-REDDIT-APP-CLIENT-ID';\r\n\r\n\/\/ client secret\r\n$clientSecret = 'YOUR-REDDIT-APP-CLIENT-SECRET';\r\n\r\n\/\/ post params \r\n$params = array(\r\n    'grant_type' =&gt; 'password',\r\n    'username' =&gt; $username,\r\n    'password' =&gt; $password\r\n);\r\n\r\n\/\/ curl settings and call to reddit\r\n$ch = curl_init( 'https:\/\/www.reddit.com\/api\/v1\/access_token' );\r\ncurl_setopt( $ch, CURLOPT_USERPWD, $clientId . ':' . $clientSecret );\r\ncurl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );\r\ncurl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'POST' );\r\ncurl_setopt( $ch, CURLOPT_POSTFIELDS, $params );\r\ncurl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );\r\n\r\n\/\/ curl response from reddit\r\n$response_raw = curl_exec( $ch );\r\n$response = json_decode( $response_raw );\r\ncurl_close($ch);\r\n\r\n\/\/ display response from reddit\r\nvar_dump( $response );<\/pre>\n<p>The &#8220;var_dump( $response )&#8221; 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.<\/p>\n<p><a href=\"https:\/\/justinstolpe.com\/blog\/wp-content\/uploads\/2019\/01\/apiresonse.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-84\" style=\"border: 1px solid #333; margin-top: 10px; margin-bottom: 10px;\" src=\"https:\/\/justinstolpe.com\/blog\/wp-content\/uploads\/2019\/01\/apiresonse.png\" alt=\"Reddit Access Token\" width=\"336\" height=\"149\" srcset=\"https:\/\/justinstolpe.com\/blog\/wp-content\/uploads\/2019\/01\/apiresonse.png 336w, https:\/\/justinstolpe.com\/blog\/wp-content\/uploads\/2019\/01\/apiresonse-300x133.png 300w\" sizes=\"auto, (max-width: 336px) 100vw, 336px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<h3><strong>Step 3: Post To A Subreddit<br \/>\n<\/strong><\/h3>\n<p>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.<\/p>\n<p>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.<\/p>\n<pre class=\"lang:php decode:true\" title=\"Post To Reddit\">\/\/ access token\r\n$accessToken = 'ACCESS-TOKEN';\r\n\r\n\/\/ access token type\r\n$accessTokenType = 'ACCESS-TOKEN-TYPE';\r\n\r\n\/\/ reddit username\r\n$username = 'YOUR-REDDIT-USERNAME';\r\n\r\n\/\/ subreddit name (no spaces)\r\n$subredditName = 'SUBREDDIT-NAME';\r\n\r\n\/\/ subreddit display name (can have spaces)\r\n$subredditDisplayName = 'SUBREDDIT-DISPLAY-NAME';\r\n\r\n\/\/ subreddit post title\r\n$subredditPostTitle = 'SUBREDDIT-POST-TITLE';\r\n\r\n\/\/subreddit post url\r\n$subredditUrl = 'SUBREDDIT-POST-URL';\r\n\r\n\/\/ api call endpoint\r\n$apiCallEndpoint = 'https:\/\/oauth.reddit.com\/api\/submit';\r\n\r\n\/\/ post data: posting a link to a subreddit\r\n$postData = array(\r\n    'url' =&gt; $subredditUrl,\r\n    'title' =&gt; $subredditPostTitle,\r\n    'sr' =&gt; $subredditName,\r\n    'kind' =&gt; 'link'\r\n);\r\n\r\n\/\/ curl settings and call to post to the subreddit\r\n$ch = curl_init( $apiCallEndpoint );\r\ncurl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );\r\ncurl_setopt( $ch, CURLOPT_USERAGENT, $subredditDisplayName . ' by \/u\/' . $username . ' (Phapper 1.0)' );\r\ncurl_setopt( $ch, CURLOPT_HTTPHEADER, array( \"Authorization: \" . $accessTokenType . \" \" . $accessToken ) );\r\ncurl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'POST' );\r\ncurl_setopt( $ch, CURLOPT_POSTFIELDS, $postData );\r\ncurl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );\r\n\r\n\/\/ curl response from our post call\r\n$response_raw = curl_exec( $ch );\r\n$response = json_decode( $response_raw );\r\ncurl_close( $ch );<\/pre>\n<p>After running the post to reddit script above, check the subreddit\u00a0 and make sure the post worked! Here is a screenshot of my successful Reddit post!<\/p>\n<p><a href=\"https:\/\/justinstolpe.com\/blog\/wp-content\/uploads\/2019\/01\/redditpost.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-97\" src=\"https:\/\/justinstolpe.com\/blog\/wp-content\/uploads\/2019\/01\/redditpost.png\" alt=\"Reddit Post\" width=\"1035\" height=\"265\" srcset=\"https:\/\/justinstolpe.com\/blog\/wp-content\/uploads\/2019\/01\/redditpost.png 1035w, https:\/\/justinstolpe.com\/blog\/wp-content\/uploads\/2019\/01\/redditpost-300x77.png 300w, https:\/\/justinstolpe.com\/blog\/wp-content\/uploads\/2019\/01\/redditpost-768x197.png 768w, https:\/\/justinstolpe.com\/blog\/wp-content\/uploads\/2019\/01\/redditpost-1024x262.png 1024w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/a><\/p>\n<p>We have successfully posted to Reddit with PHP using their API! Here are some helpful links relating to this blog post.<\/p>\n<p><a href=\"https:\/\/www.youtube.com\/watch?v=1Prmvgu-Wlo\" target=\"_blank\" rel=\"noopener\"><strong>YouTube Video<\/strong><\/a><\/p>\n<p><strong><a href=\"https:\/\/github.com\/jstolpe\/blog_code\/tree\/master\/reddit_api_php\" target=\"_blank\" rel=\"noopener\">Code on GitHub<\/a><\/strong><\/p>\n<p><a href=\"https:\/\/www.reddit.com\/dev\/api\/\" target=\"_blank\" rel=\"noopener\"><strong>Reddit API Documentation<\/strong><\/a><\/p>\n<p>That is going to do it for this post! Leave any comments\/questions\/concerns below and thanks for stopping by the blog!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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. &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/justinstolpe.com\/blog\/2019\/01\/13\/reddit-api-with-php\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Reddit API with PHP&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":104,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,9],"tags":[7,8],"class_list":["post-56","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-coding","category-php","tag-coding","tag-php"],"_links":{"self":[{"href":"https:\/\/justinstolpe.com\/blog\/wp-json\/wp\/v2\/posts\/56","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/justinstolpe.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/justinstolpe.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/justinstolpe.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/justinstolpe.com\/blog\/wp-json\/wp\/v2\/comments?post=56"}],"version-history":[{"count":46,"href":"https:\/\/justinstolpe.com\/blog\/wp-json\/wp\/v2\/posts\/56\/revisions"}],"predecessor-version":[{"id":112,"href":"https:\/\/justinstolpe.com\/blog\/wp-json\/wp\/v2\/posts\/56\/revisions\/112"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/justinstolpe.com\/blog\/wp-json\/wp\/v2\/media\/104"}],"wp:attachment":[{"href":"https:\/\/justinstolpe.com\/blog\/wp-json\/wp\/v2\/media?parent=56"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/justinstolpe.com\/blog\/wp-json\/wp\/v2\/categories?post=56"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/justinstolpe.com\/blog\/wp-json\/wp\/v2\/tags?post=56"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}