Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Reddit PHP SDK
- *
- * Provides a SDK for accessing the Reddit APIs
- * Useage:
- * $reddit = new reddit();
- * $reddit->login("USERNAME", "PASSWORD");
- * $user = $reddit->getUser();
- */
- class reddit{
- private $apiHost = "https://ssl.reddit.com/api";
- // private $username = null;
- private $modHash = null;
- private $session = null;
- private $username = null;
- private $id = null;
- /**
- * Class Constructor
- *
- * Construct the class and simultaneously log a user in.
- * @link https://github.com/reddit/reddit/wiki/API%3A-login
- * @param string $username The username to be logged into
- * @param string $password The password to be used to log in
- */
- public function __construct($username = null, $password = null, $id = null)
- {
- $urlLogin = "{$this->apiHost}/login/$username";
- $postData = sprintf("api_type=json&user=%s&passwd=%s",
- $username,
- $password);
- $response = $this->runCurl($urlLogin, $postData);
- if (!is_object($response) || !is_object($response->json) || !is_object($response->json->data)) return false;
- else {
- $this->modHash = $response->json->data->modhash;
- $this->session = $response->json->data->cookie;
- $this->username = $username;
- $this->id = $id;
- return $this->modHash;
- }
- }
- /**
- * removeStory
- *
- * Removes post by ID
- */
- public function approveStory($name){
- $urlSubmit = "{$this->apiHost}/approve";
- $postData = sprintf("uh=%s&id=%s", $this->modHash, $name);
- $response = $this->runCurl($urlSubmit, $postData);
- return $response;
- }
- public function removeStory($name){
- $urlSubmit = "{$this->apiHost}/remove";
- $postData = sprintf("uh=%s&id=%s&spam=false", $this->modHash, $name);
- $response = $this->runCurl($urlSubmit, $postData);
- return $response;
- }
- public function deleteStory($name){
- $urlSubmit = "{$this->apiHost}/del";
- $postData = sprintf("uh=%s&id=%s", $this->modHash, $name);
- $response = $this->runCurl($urlSubmit, $postData);
- return $response;
- }
- public function createStory($title = null, $link = null, $subreddit = null, $selftext = null){
- $urlSubmit = "{$this->apiHost}/submit";
- //data checks and pre-setup
- if ($title == null || $subreddit == null) { return null; }
- $kind = ($link == null) ? "self" : "link";
- $postData = sprintf("uh=%s&kind=%s&sr=%s&title=%s&r=%s&renderstyle=html",
- $this->modHash,
- $kind,
- $subreddit,
- urlencode($title),
- $subreddit);
- //if link was present, add to POST data
- if ($link != null){ $postData .= "&url=" . urlencode($link); }
- else if ($selftext != null){ $postData .= "&text=" . urlencode($selftext); }
- $response = $this->runCurl($urlSubmit, $postData);
- if (isset($response->jquery)) return $response->jquery; // [18][0];
- else return false;
- //if ($response->jquery[18][3][0] == "that link has already been submitted"){
- // return $response->jquery[18][3][0];
- //}
- }
- public function sendMessage($to, $subj, $message)
- {
- $postData = sprintf("api_type=json&uh=%s&to=%s&subject=%s&text=%s", $this->modHash, $to, $subj, $message);
- $response = $this->runCurl("{$this->apiHost}/compose", $postData);
- return $response;
- }
- /**
- * Get user
- *
- * Get data for the current user
- * @link https://github.com/reddit/reddit/wiki/API%3A-me.json
- */
- public function getUser(){
- $urlUser = "{$this->apiHost}/me.json";
- return $this->runCurl($urlUser);
- }
- public function addFriend($name)
- {
- $urlFriend = "{$this->apiHost}/friend";
- $postData = sprintf("uh=%s&container=%s&name=%s&type=friend", $this->modHash, $this->id, $name);
- $response = $this->runCurl($urlFriend, $postData);
- return $response;
- }
- public function banUser($subid, $subname, $name)
- {
- $urlBan = "{$this->apiHost}/friend";
- $postData = sprintf("uh=%s&container=%s&r=%s&name=%s&type=banned&action=add", $this->modHash, $subid, $subname, $name);
- $response = $this->runCurl($urlBan, $postData);
- return $response;
- }
- /**
- * Get user subscriptions
- *
- * Get the subscriptions that the user is subscribed to
- * @link https://github.com/reddit/reddit/wiki/API%3A-mine.json
- */
- public function getSubscriptions(){
- $urlSubscriptions = "https://www.reddit.com/reddits/mine.json";
- return $this->runCurl($urlSubscriptions);
- }
- public function getModSubreddits(){
- $urlSubscriptions = "https://www.reddit.com/reddits/mine/moderator.json";
- return $this->runCurl($urlSubscriptions);
- }
- public function modAccept($subname)
- {
- $urlBan = "{$this->apiHost}/accept_moderator_invite";
- $postData = sprintf("uh=%s&r=%s", $this->modHash, $subname);
- $response = $this->runCurl($urlBan, $postData);
- return $response;
- }
- /* no work
- public function setPrivate($sr){
- $urlSubmit = "{$this->apiHost}/site_admin";
- $postData = sprintf("uh=%s&sr=%s&type=private", $this->modHash, $sr);
- $response = $this->runCurl($urlSubmit, $postData);
- return $response;
- }
- public function setRestricted($sr){
- $urlSubmit = "{$this->apiHost}/site_admin";
- $postData = sprintf("uh=%s&sr=%s&type=restricted", $this->modHash, $sr);
- $response = $this->runCurl($urlSubmit, $postData);
- return $response;
- }
- */
- public function getMessages($count = 5) {
- $getMsgsUn = "https://www.reddit.com/message/inbox.json"; // ?mark=true&limit=" . ;
- $postData = sprintf("uh=%s&id=%s&limit=", $this->modHash, $this->id, $count);
- $response = $this->runCurl($getMsgsUn . "?" . $postData); //, $postData);
- //$response = $this->runCurl($getMsgsUn, $postData); //, $postData);
- return $response;
- }
- public function getMessagesUnread($count = 10) {
- $getMsgsUn = "https://www.reddit.com/message/unread.json"; // ?mark=true&limit=" . ;
- $postData = sprintf("mark=true&uh=%s&id=%s&limit=", $this->modHash, $this->id, $count);
- $response = $this->runCurl($getMsgsUn . "?" . $postData); //, $postData);
- //$response = $this->runCurl($getMsgsUn, $postData); //, $postData);
- return $response;
- }
- /**
- * Get listing
- *
- * Get the listing of submissions from a subreddit
- * @link https://www.reddit.com/dev/api#GET_listing
- * @param string $sr The subreddit name. Ex: technology, limit (integer): The number of posts to gather
- */
- public function getListing($sr, $limit = 5){
- $limit = (isset($limit)) ? "?limit=".$limit : "";
- if($sr == 'home' || $sr == 'reddit' || !isset($sr)){
- $urlListing = "https://www.reddit.com/.json{$limit}";
- } else if (strpos($sr, "?") !== false) {
- $qPos = strpos($sr, "?");
- $urlListing = "https://www.reddit.com/" . substr($sr, 0, $qPos) . ".json{$limit}&" . substr($sr, $qPos + 1);
- } else {
- $urlListing = "https://www.reddit.com/{$sr}.json{$limit}";
- }
- return $this->runCurl($urlListing);
- }
- /**
- * Get page information
- *
- * Get information on a URLs submission on Reddit
- * @link https://github.com/reddit/reddit/wiki/API%3A-info.json
- * @param string $url The URL to get information for
- */
- public function getPageInfo($url){
- $response = null;
- if ($url){
- $urlInfo = "{$this->apiHost}/info.json?url=" . urlencode($url);
- $response = $this->runCurl($urlInfo);
- }
- return $response;
- }
- /**
- * Get Raw JSON
- *
- * Get Raw JSON for a reddit permalink
- * @param string $permalink permalink to get raw JSON for
- */
- public function getRawJSON($permalink, $limit = 25) {
- $limit = (isset($limit)) ? "?limit=".$limit : "";
- if (strpos($permalink, "?") !== false) {
- $qPos = strpos($permalink, "?");
- $urlListing = "https://www.reddit.com/" . substr($permalink, 0, $qPos) . ".json{$limit}&" . substr($permalink, $qPos + 1);
- } else {
- $urlListing = "https://www.reddit.com/{$permalink}.json{$limit}";
- }
- ;
- return $this->runCurl($urlListing);
- }
- /**
- * Save post
- *
- * Save a post to your account. Save feeds:
- * https://www.reddit.com/saved/.xml
- * https://www.reddit.com/saved/.json
- * @link https://github.com/reddit/reddit/wiki/API%3A-save
- * @param string $name the full name of the post to save (name parameter
- * in the getSubscriptions() return value)
- */
- public function savePost($name){
- $response = null;
- if ($name){
- $urlSave = "{$this->apiHost}/save";
- $postData = sprintf("id=%s&uh=%s", $name, $this->modHash);
- $response = $this->runCurl($urlSave, $postData);
- }
- return $response;
- }
- /**
- * Unsave post
- *
- * Unsave a saved post from your account
- * @link https://github.com/reddit/reddit/wiki/API%3A-unsave
- * @param string $name the full name of the post to unsave (name parameter
- * in the getSubscriptions() return value)
- */
- public function unsavePost($name){
- $response = null;
- if ($name){
- $urlUnsave = "{$this->apiHost}/unsave";
- $postData = sprintf("id=%s&uh=%s", $name, $this->modHash);
- $response = $this->runCurl($urlUnsave, $postData);
- }
- return $response;
- }
- /**
- * Get saved posts
- *
- * Get the listing of a user's saved posts
- * @param string $username the desired user. Must be already authenticated.
- */
- public function getSaved($username){
- return $this->runCurl("https://www.reddit.com/user/".$username."/saved.json");
- }
- /**
- * Hide post
- *
- * Hide a post on your account
- * @link https://github.com/reddit/reddit/wiki/API%3A-hide
- * @param string $name The full name of the post to hide (name parameter
- * in the getSubscriptions() return value)
- */
- public function hidePost($name){
- $response = null;
- if ($name){
- $urlHide = "{$this->apiHost}/hide";
- $postData = sprintf("id=%s&uh=%s", $name, $this->modHash);
- $response = $this->runCurl($urlHide, $postData);
- }
- return $response;
- }
- /**
- * Unhide post
- *
- * Unhide a hidden post on your account
- * @link https://github.com/reddit/reddit/wiki/API%3A-unhide
- * @param string $name The full name of the post to unhide (name parameter
- * in the getSubscriptions() return value)
- */
- public function unhidePost($name){
- $response = null;
- if ($name){
- $urlUnhide = "{$this->apiHost}/unhide";
- $postData = sprintf("id=%s&uh=%s", $name, $this->modHash);
- $response = $this->runCurl($urlUnhide, $postData);
- }
- return $response;
- }
- public function reportThing($name, $reason = "")
- {
- $response = null;
- if ($name) {
- $postData = sprintf("api_type=json&thing_id=%s&reason=other&other_reason=%s&uh=%s", $name, $reason, $this->modHash);
- $response = $this->runCurl("{$this->apiHost}/report", $postData);
- }
- return $response;
- }
- /**
- * Share a post
- *
- * E-Mail a post to someone
- * @link https://github.com/reddit/reddit/wiki/API
- * @param string $name The full name of the post to share (name parameter
- * in the getSubscriptions() return value)
- * @param string $shareFrom The name of the person sharing the story
- * @param string $replyTo The e-mail the sharee should respond to
- * @param string $shareTo The e-mail the story should be sent to
- * @param string $message The e-mail message
- */
- public function sharePost($name, $shareFrom, $replyTo, $shareTo, $message){
- $urlShare = "{$this->apiHost}/share";
- $postData = sprintf("parent=%s&share_from=%s&replyto=%s&share_to=%s&message=%s&uh=%s",
- $name,
- $shareFrom,
- $replyTo,
- $shareTo,
- $message,
- $this->modHash);
- $response = $this->runCurl($urlShare, $postData);
- return $response;
- }
- /**
- * Add new comment
- *
- * Add a new comment to a story
- * @link https://github.com/reddit/reddit/wiki/API%3A-comment
- * @param string $name The full name of the post to comment (name parameter
- * in the getSubscriptions() return value)
- * @param string $text The comment markup
- */
- public function addComment($name, $text){
- $response = null;
- if ($name && $text){
- $urlComment = "{$this->apiHost}/comment";
- $postData = sprintf("thing_id=%s&text=%s&uh=%s",
- $name,
- $text,
- $this->modHash);
- $response = $this->runCurl($urlComment, $postData);
- }
- return $response;
- }
- public function distinguish($name){
- $response = null;
- if ($name) {
- $urlEdit = "{$this->apiHost}/distinguish";
- $postData = sprintf("api_type=json&how=yes&id=%s&uh=%s",
- $name,
- $this->modHash);
- $response = $this->runCurl($urlEdit, $postData);
- }
- return $response;
- }
- public function editThing($name, $text){
- $response = null;
- if ($name && $text){
- $urlEdit = "{$this->apiHost}/editusertext";
- $postData = sprintf("api_type=json&thing_id=%s&text=%s&uh=%s",
- $name,
- $text,
- $this->modHash);
- $response = $this->runCurl($urlEdit, $postData);
- }
- return $response;
- }
- /**
- * Vote on a story
- *
- * Adds a vote (up / down / neutral) on a story
- * @link https://github.com/reddit/reddit/wiki/API%3A-vote
- * @param string $name The full name of the post to vote on (name parameter
- * in the getSubscriptions() return value)
- * @param int $vote The vote to be made (1 = upvote, 0 = no vote,
- * -1 = downvote)
- */
- public function addVote($name, $vote = 1){
- $response = null;
- if ($name){
- $urlVote = "{$this->apiHost}/vote";
- $postData = sprintf("id=%s&dir=%s&uh=%s", $name, $vote, $this->modHash);
- $response = $this->runCurl($urlVote, $postData);
- }
- return $response;
- }
- /**
- * Set flair
- *
- * Set or clear a user's flair in a subreddit
- * @link https://github.com/reddit/reddit/wiki/API%3A-flair
- * @param string $subreddit The subreddit to use
- * @param string $user The name of the userLink
- * @param string $text Flair text to assign
- */
- public function setFlair($subreddit, $user, $text, $cssClass){
- $urlFlair = "{$this->apiHost}/flair";
- $postData = sprintf("r=%s&name=%s&text=%s&css_class=%s&uh=%s",
- $subreddit,
- $user,
- $text,
- $cssClass,
- $this->modHash);
- $response = $this->runCurl($urlFlair, $postData);
- return $response;
- }
- public function setLinkFlair($subreddit, $post, $flair_template_id, $overwrite = false)
- {
- $changeFlair = true;
- if ($overwrite === false) {
- $flairCheck = $this->getRawJSON("by_id/" . $post);
- if (!isset($flairCheck->error) && strcmp($flairCheck->kind, "Listing") === 0 && isset($flairCheck->data->children[0])) {
- $flairPost = $flairCheck->data->children[0]->data;
- // print_r($flairPost);
- // echo "class: " . $flairPost->link_flair_css_class . " " . strcmp($flairPost->link_flair_css_class, "") . chr(10);
- // echo "text: " . $flairPost->link_flair_text . " " . strcmp($flairPost->link_flair_text, "") . chr(10);
- if (
- (strcmp($flairPost->link_flair_css_class, "") !== 0 || strcmp($flairPost->link_flair_text, "") !== 0)
- && strcmp($flairPost->link_flair_css_class, "curated") !== 0
- // && strcmp($flairPost->link_flair_text, "curated") !== 0
- ) $changeFlair = false;
- }
- }
- // echo "change: " . $changeFlair . chr(10);
- if ($changeFlair) {
- $urlFlair = "{$this->apiHost}/selectflair";
- $postData = sprintf("r=%s&flair_template_id=%s&link=%s&uh=%s"
- ,$subreddit
- ,$flair_template_id
- ,$post
- ,$this->modHash
- );
- $response = $this->runCurl($urlFlair, $postData);
- return $response;
- }
- else return '{"skipped":"flair already exists"}';
- }
- /**
- * Get flair list
- *
- * Download the flair assignments of a subreddit
- * @link https://github.com/reddit/reddit/wiki/API%3A-flairlist
- * @param string $subreddit The subreddit to use
- * @param int $limit The maximum number of items to return (max 1000)
- * @param string $after Return entries starting after this user
- * @param string $before Return entries starting before this user
- */
- public function getFlairList($subreddit, $limit = 100, $after, $before){
- $urlFlairList = "{$this->apiHost}/share";
- $postData = sprintf("r=%s&limit=%s&after=%s&before=%s&uh=%s",
- $subreddit,
- $limit,
- $after,
- $before,
- $this->modHash);
- $response = $this->runCurl($urlFlairList, $postData);
- return $response;
- }
- /**
- * Set flair CSV file
- *
- * Post a CSV file of flair settings to a subreddit
- * @link https://github.com/reddit/reddit/wiki/API%3A-flaircsv
- * @param string $subreddit The subreddit to use
- * @param string $flairCSV CSV file contents, up to 100 lines
- */
- public function setFlairCSV($subreddit, $flairCSV){
- $urlFlairCSV = "{$this->apiHost}/flaircsv.json";
- $postData = sprintf("r=%s&flair_csv=%s&uh=%s",
- $subreddit,
- $flairCSV,
- $this->modHash);
- $response = $this->runCurl($urlFlairCSV, $postData);
- return $response;
- }
- /*
- POST [/r/subreddit]/api/wiki/edit
- content
- page -- the name of an existing page or a new page to create
- previous -- the starting point revision for this edit
- reason -- a string up to 256 characters long, consisting of printable characters.
- uh -- modhash
- */
- public function setWikiPage($subreddit, $page, $content) {
- $urlWiki = "{$this->apiHost}/wiki/edit";
- $postData = sprintf("r=%s&uh=%s&page=%s&content=%s",
- $subreddit
- ,$this->modHash
- ,$page
- ,$content
- );
- $response = $this->runCurl($urlWiki, $postData);
- return $response;
- }
- public function setSubProperty($sr, $prop, $val) {
- $settings = $this->getRawJSON("r/" . $sr . "/about/edit");
- if (empty($settings->data)) return false;
- else if ($prop == "link_type") $prop == "content_options";
- if (isset($settings->data->{$prop})) $settings->data->{$prop} = $val;
- else { echo "Invalid subreddit property: " . $prop . chr(10); print_r($settings); die(); }
- $settings->data->uh = $this->modHash;
- $settings->data->sr = $settings->data->subreddit_id;
- $settings->data->type = $settings->data->subreddit_type;
- $settings->data->link_type = $settings->data->content_options;
- foreach ($settings->data as $key => $value)
- if (is_string($value)) $settings->data->{$key} = html_entity_decode($value);
- $urlSubmit = "{$this->apiHost}/site_admin";
- $response = $this->runCurl($urlSubmit, $settings->data);
- return $response;
- }
- ///////////////////////////////////////
- /**
- * cURL request
- *
- * General cURL request function for GET and POST
- * @link URL
- * @param string $url URL to be requested
- * @param string $postVals NVP string to be send with POST request
- */
- private function runCurl($url, $postVals = null, $tryCnt = 1)
- {
- // echo "CURL " . $url . chr(10);
- $ch = curl_init($url);
- $options = array(
- CURLOPT_RETURNTRANSFER => true
- ,CURLOPT_COOKIE => "reddit_session={$this->session}"
- ,CURLOPT_TIMEOUT => 10
- ,CURLOPT_USERAGENT => 'bot ' . $this->username + ' / radd.it data services by /u/radd_it'
- ,CURLOPT_FAILONERROR => true
- );
- if ($postVals != null){
- $options[CURLOPT_POSTFIELDS] = $postVals;
- $options[CURLOPT_CUSTOMREQUEST] = "POST";
- }
- curl_setopt_array($ch, $options);
- $response = curl_exec($ch);
- if ($response === false) {
- $toErrIsReddit = curl_error($ch);
- curl_close($ch);
- if (strpos($toErrIsReddit, "error: 403") !== false) { $response = new stdClass(); $response->error = "403"; } // echo "403: " . $url . chr(10);
- else if (strpos($toErrIsReddit, "error: 404") !== false) { $response = new stdClass(); $response->error = "404"; } // echo "404: " . $url . chr(10);
- else if (
- strcmp(
- $toErrIsReddit
- , str_replace(
- array(
- "SSL read:", "SSL_write()", " timed out", "error: 502", "error: 503", "error: 504", "error: 520"
- , "connection timeout", " reset by peer", "Empty reply"
- )
- , ""
- , $toErrIsReddit
- )
- ) !== 0
- ) {
- if ($tryCnt > 2) die(date("M d, Y G:i") . " 3 sequential failures. Last error: " . $toErrIsReddit);
- // echo "(" . $tryCnt . ") " . $toErrIsReddit . chr(10);
- sleep(2);
- $response = $this->runCurl($url, $postVals, $tryCnt + 1);
- }
- else die(date("M d, Y G:i") . " " . $url . " failed, error: " . $toErrIsReddit);
- }
- else {
- $response = json_decode($response);
- curl_close($ch);
- sleep(3);
- }
- return $response;
- }
- }
- ?>
Add Comment
Please, Sign In to add comment