Guest User

Untitled

a guest
Aug 26th, 2016
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 23.56 KB | None | 0 0
  1. <?php
  2. /**
  3. * Reddit PHP SDK
  4. *
  5. * Provides a SDK for accessing the Reddit APIs
  6. * Useage:
  7. *   $reddit = new reddit();
  8. *   $reddit->login("USERNAME", "PASSWORD");
  9. *   $user = $reddit->getUser();
  10. */
  11. class reddit{
  12.     private $apiHost = "https://ssl.reddit.com/api";
  13.     // private $username = null;
  14.     private $modHash = null;
  15.     private $session = null;
  16.     private $username = null;
  17.     private $id = null;
  18.    
  19.     /**
  20.     * Class Constructor
  21.     *
  22.     * Construct the class and simultaneously log a user in.
  23.     * @link https://github.com/reddit/reddit/wiki/API%3A-login
  24.     * @param string $username The username to be logged into
  25.     * @param string $password The password to be used to log in
  26.     */
  27.     public function __construct($username = null, $password = null, $id = null)
  28.     {
  29.         $urlLogin = "{$this->apiHost}/login/$username";
  30.        
  31.         $postData = sprintf("api_type=json&user=%s&passwd=%s",
  32.                             $username,
  33.                             $password);
  34.         $response = $this->runCurl($urlLogin, $postData);
  35.        
  36.         if (!is_object($response) || !is_object($response->json) || !is_object($response->json->data)) return false;  
  37.         else {
  38.             $this->modHash = $response->json->data->modhash;
  39.             $this->session = $response->json->data->cookie;
  40.             $this->username = $username;
  41.             $this->id = $id;
  42.  
  43.             return $this->modHash;
  44.         }
  45.     }
  46.    
  47.     /**
  48.     * removeStory
  49.     *
  50.     * Removes post by ID
  51.     */
  52.     public function approveStory($name){
  53.         $urlSubmit = "{$this->apiHost}/approve";
  54.         $postData = sprintf("uh=%s&id=%s", $this->modHash, $name);
  55.         $response = $this->runCurl($urlSubmit, $postData);
  56.         return $response;
  57.     }
  58.  
  59.     public function removeStory($name){
  60.         $urlSubmit = "{$this->apiHost}/remove";
  61.         $postData = sprintf("uh=%s&id=%s&spam=false", $this->modHash, $name);
  62.         $response = $this->runCurl($urlSubmit, $postData);
  63.         return $response;
  64.     }
  65.  
  66.     public function deleteStory($name){
  67.         $urlSubmit = "{$this->apiHost}/del";
  68.         $postData = sprintf("uh=%s&id=%s", $this->modHash, $name);
  69.         $response = $this->runCurl($urlSubmit, $postData);
  70.         return $response;
  71.     }
  72.  
  73.     public function createStory($title = null, $link = null, $subreddit = null, $selftext = null){
  74.         $urlSubmit = "{$this->apiHost}/submit";
  75.        
  76.         //data checks and pre-setup
  77.         if ($title == null || $subreddit == null) { return null; }
  78.         $kind = ($link == null) ? "self" : "link";
  79.        
  80.         $postData = sprintf("uh=%s&kind=%s&sr=%s&title=%s&r=%s&renderstyle=html",
  81.                             $this->modHash,
  82.                             $kind,
  83.                             $subreddit,
  84.                             urlencode($title),
  85.                             $subreddit);
  86.        
  87.         //if link was present, add to POST data            
  88.         if ($link != null){ $postData .= "&url=" . urlencode($link); }
  89.         else if ($selftext != null){ $postData .= "&text=" . urlencode($selftext); }
  90.    
  91.         $response = $this->runCurl($urlSubmit, $postData);
  92.         if (isset($response->jquery)) return $response->jquery; // [18][0];
  93.         else return false;
  94.         //if ($response->jquery[18][3][0] == "that link has already been submitted"){
  95.         //    return $response->jquery[18][3][0];
  96.         //}
  97.     }
  98.  
  99.     public function sendMessage($to, $subj, $message)
  100.     {
  101.         $postData = sprintf("api_type=json&uh=%s&to=%s&subject=%s&text=%s", $this->modHash, $to, $subj, $message);
  102.         $response = $this->runCurl("{$this->apiHost}/compose", $postData);
  103.         return $response;
  104.     }
  105.  
  106.     /**
  107.     * Get user
  108.     *
  109.     * Get data for the current user
  110.     * @link https://github.com/reddit/reddit/wiki/API%3A-me.json
  111.     */
  112.     public function getUser(){
  113.         $urlUser = "{$this->apiHost}/me.json";
  114.         return $this->runCurl($urlUser);
  115.     }
  116.    
  117.     public function addFriend($name)
  118.     {
  119.         $urlFriend = "{$this->apiHost}/friend";
  120.         $postData = sprintf("uh=%s&container=%s&name=%s&type=friend", $this->modHash, $this->id, $name);
  121.         $response = $this->runCurl($urlFriend, $postData);
  122.         return $response;
  123.     }
  124.  
  125.     public function banUser($subid, $subname, $name)
  126.     {
  127.         $urlBan = "{$this->apiHost}/friend";
  128.         $postData = sprintf("uh=%s&container=%s&r=%s&name=%s&type=banned&action=add", $this->modHash, $subid, $subname, $name);
  129.         $response = $this->runCurl($urlBan, $postData);
  130.         return $response;
  131.     }
  132.  
  133.     /**
  134.     * Get user subscriptions
  135.     *
  136.     * Get the subscriptions that the user is subscribed to
  137.     * @link https://github.com/reddit/reddit/wiki/API%3A-mine.json
  138.     */
  139.     public function getSubscriptions(){
  140.         $urlSubscriptions = "https://www.reddit.com/reddits/mine.json";
  141.         return $this->runCurl($urlSubscriptions);
  142.     }
  143.    
  144.     public function getModSubreddits(){
  145.         $urlSubscriptions = "https://www.reddit.com/reddits/mine/moderator.json";
  146.         return $this->runCurl($urlSubscriptions);
  147.     }
  148.    
  149.     public function modAccept($subname)
  150.     {
  151.         $urlBan = "{$this->apiHost}/accept_moderator_invite";
  152.         $postData = sprintf("uh=%s&r=%s", $this->modHash, $subname);
  153.         $response = $this->runCurl($urlBan, $postData);
  154.         return $response;
  155.     }
  156.  
  157. /* no work
  158.     public function setPrivate($sr){
  159.         $urlSubmit = "{$this->apiHost}/site_admin";
  160.         $postData = sprintf("uh=%s&sr=%s&type=private", $this->modHash, $sr);
  161.         $response = $this->runCurl($urlSubmit, $postData);
  162.         return $response;
  163.     }
  164.    
  165.     public function setRestricted($sr){
  166.         $urlSubmit = "{$this->apiHost}/site_admin";
  167.         $postData = sprintf("uh=%s&sr=%s&type=restricted", $this->modHash, $sr);
  168.         $response = $this->runCurl($urlSubmit, $postData);
  169.         return $response;
  170.     }
  171. */
  172.  
  173.     public function getMessages($count = 5) {
  174.         $getMsgsUn = "https://www.reddit.com/message/inbox.json"; // ?mark=true&limit=" . ;
  175.         $postData = sprintf("uh=%s&id=%s&limit=", $this->modHash, $this->id, $count);
  176.         $response = $this->runCurl($getMsgsUn . "?" . $postData); //, $postData);
  177.         //$response = $this->runCurl($getMsgsUn, $postData); //, $postData);
  178.  
  179.         return $response;
  180.     }
  181.         public function getMessagesUnread($count = 10) {
  182.         $getMsgsUn = "https://www.reddit.com/message/unread.json"; // ?mark=true&limit=" . ;
  183.         $postData = sprintf("mark=true&uh=%s&id=%s&limit=", $this->modHash, $this->id, $count);
  184.         $response = $this->runCurl($getMsgsUn . "?" . $postData); //, $postData);
  185.         //$response = $this->runCurl($getMsgsUn, $postData); //, $postData);
  186.  
  187.         return $response;
  188.     }
  189.    
  190.  
  191.     /**
  192.     * Get listing
  193.     *
  194.     * Get the listing of submissions from a subreddit
  195.     * @link https://www.reddit.com/dev/api#GET_listing
  196.     * @param string $sr The subreddit name. Ex: technology, limit (integer): The number of posts to gather
  197.     */
  198.     public function getListing($sr, $limit = 5){
  199.         $limit = (isset($limit)) ? "?limit=".$limit : "";
  200.         if($sr == 'home' || $sr == 'reddit' || !isset($sr)){
  201.             $urlListing = "https://www.reddit.com/.json{$limit}";
  202.         } else if (strpos($sr, "?") !== false) {
  203.             $qPos = strpos($sr, "?");
  204.             $urlListing = "https://www.reddit.com/" . substr($sr, 0, $qPos) . ".json{$limit}&" . substr($sr, $qPos + 1);
  205.         } else {
  206.             $urlListing = "https://www.reddit.com/{$sr}.json{$limit}";
  207.         }
  208.         return $this->runCurl($urlListing);
  209.     }
  210.    
  211.     /**
  212.     * Get page information
  213.     *
  214.     * Get information on a URLs submission on Reddit
  215.     * @link https://github.com/reddit/reddit/wiki/API%3A-info.json
  216.     * @param string $url The URL to get information for
  217.     */
  218.     public function getPageInfo($url){
  219.         $response = null;
  220.         if ($url){
  221.             $urlInfo = "{$this->apiHost}/info.json?url=" . urlencode($url);
  222.             $response = $this->runCurl($urlInfo);
  223.         }
  224.         return $response;
  225.     }
  226.    
  227.     /**
  228.     * Get Raw JSON
  229.     *
  230.     * Get Raw JSON for a reddit permalink
  231.     * @param string $permalink permalink to get raw JSON for
  232.     */
  233.     public function getRawJSON($permalink, $limit = 25) {
  234.         $limit = (isset($limit)) ? "?limit=".$limit : "";
  235.  
  236.         if (strpos($permalink, "?") !== false) {
  237.             $qPos = strpos($permalink, "?");
  238.             $urlListing = "https://www.reddit.com/" . substr($permalink, 0, $qPos) . ".json{$limit}&" . substr($permalink, $qPos + 1);
  239.         } else {
  240.             $urlListing = "https://www.reddit.com/{$permalink}.json{$limit}";
  241.         }
  242. ;
  243.         return $this->runCurl($urlListing);
  244.     }  
  245.          
  246.     /**
  247.     * Save post
  248.     *
  249.     * Save a post to your account.  Save feeds:
  250.     * https://www.reddit.com/saved/.xml
  251.     * https://www.reddit.com/saved/.json
  252.     * @link https://github.com/reddit/reddit/wiki/API%3A-save
  253.     * @param string $name the full name of the post to save (name parameter
  254.     *                     in the getSubscriptions() return value)
  255.     */
  256.     public function savePost($name){
  257.         $response = null;
  258.         if ($name){
  259.             $urlSave = "{$this->apiHost}/save";
  260.             $postData = sprintf("id=%s&uh=%s", $name, $this->modHash);
  261.             $response = $this->runCurl($urlSave, $postData);
  262.         }
  263.         return $response;
  264.     }
  265.    
  266.     /**
  267.     * Unsave post
  268.     *
  269.     * Unsave a saved post from your account
  270.     * @link https://github.com/reddit/reddit/wiki/API%3A-unsave
  271.     * @param string $name the full name of the post to unsave (name parameter
  272.     *                     in the getSubscriptions() return value)
  273.     */
  274.     public function unsavePost($name){
  275.         $response = null;
  276.         if ($name){
  277.             $urlUnsave = "{$this->apiHost}/unsave";
  278.             $postData = sprintf("id=%s&uh=%s", $name, $this->modHash);
  279.             $response = $this->runCurl($urlUnsave, $postData);
  280.         }
  281.         return $response;
  282.     }
  283.    
  284.     /**
  285.     * Get saved posts
  286.     *
  287.     * Get the listing of a user's saved posts
  288.     * @param string $username the desired user. Must be already authenticated.
  289.     */
  290.     public function getSaved($username){
  291.         return $this->runCurl("https://www.reddit.com/user/".$username."/saved.json");
  292.     }
  293.    
  294.     /**
  295.     * Hide post
  296.     *
  297.     * Hide a post on your account
  298.     * @link https://github.com/reddit/reddit/wiki/API%3A-hide
  299.     * @param string $name The full name of the post to hide (name parameter
  300.     *                     in the getSubscriptions() return value)
  301.     */
  302.     public function hidePost($name){
  303.         $response = null;
  304.         if ($name){
  305.             $urlHide = "{$this->apiHost}/hide";
  306.             $postData = sprintf("id=%s&uh=%s", $name, $this->modHash);
  307.             $response = $this->runCurl($urlHide, $postData);
  308.         }
  309.         return $response;
  310.     }
  311.    
  312.     /**
  313.     * Unhide post
  314.     *
  315.     * Unhide a hidden post on your account
  316.     * @link https://github.com/reddit/reddit/wiki/API%3A-unhide
  317.     * @param string $name The full name of the post to unhide (name parameter
  318.     *                     in the getSubscriptions() return value)
  319.     */
  320.     public function unhidePost($name){
  321.         $response = null;
  322.         if ($name){
  323.             $urlUnhide = "{$this->apiHost}/unhide";
  324.             $postData = sprintf("id=%s&uh=%s", $name, $this->modHash);
  325.             $response = $this->runCurl($urlUnhide, $postData);
  326.         }
  327.         return $response;
  328.     }
  329.  
  330.     public function reportThing($name, $reason = "")
  331.     {
  332.         $response = null;
  333.         if ($name) {
  334.             $postData = sprintf("api_type=json&thing_id=%s&reason=other&other_reason=%s&uh=%s", $name, $reason, $this->modHash);
  335.             $response = $this->runCurl("{$this->apiHost}/report", $postData);
  336.         }
  337.         return $response;
  338.     }
  339.    
  340.     /**
  341.     * Share a post
  342.     *
  343.     * E-Mail a post to someone
  344.     * @link https://github.com/reddit/reddit/wiki/API
  345.     * @param string $name The full name of the post to share (name parameter
  346.     *                     in the getSubscriptions() return value)
  347.     * @param string $shareFrom The name of the person sharing the story
  348.     * @param string $replyTo The e-mail the sharee should respond to
  349.     * @param string $shareTo The e-mail the story should be sent to
  350.     * @param string $message The e-mail message
  351.     */
  352.     public function sharePost($name, $shareFrom, $replyTo, $shareTo, $message){
  353.         $urlShare = "{$this->apiHost}/share";
  354.         $postData = sprintf("parent=%s&share_from=%s&replyto=%s&share_to=%s&message=%s&uh=%s",
  355.                             $name,
  356.                             $shareFrom,
  357.                             $replyTo,
  358.                             $shareTo,
  359.                             $message,
  360.                             $this->modHash);
  361.        
  362.         $response = $this->runCurl($urlShare, $postData);
  363.         return $response;
  364.     }
  365.    
  366.     /**
  367.     * Add new comment
  368.     *
  369.     * Add a new comment to a story
  370.     * @link https://github.com/reddit/reddit/wiki/API%3A-comment
  371.     * @param string $name The full name of the post to comment (name parameter
  372.     *                     in the getSubscriptions() return value)
  373.     * @param string $text The comment markup
  374.     */
  375.     public function addComment($name, $text){
  376.         $response = null;
  377.         if ($name && $text){
  378.             $urlComment = "{$this->apiHost}/comment";
  379.             $postData = sprintf("thing_id=%s&text=%s&uh=%s",
  380.                                 $name,
  381.                                 $text,
  382.                                 $this->modHash);
  383.             $response = $this->runCurl($urlComment, $postData);
  384.         }
  385.         return $response;
  386.     }
  387.    
  388.     public function distinguish($name){
  389.         $response = null;
  390.         if ($name) {
  391.             $urlEdit = "{$this->apiHost}/distinguish";
  392.             $postData = sprintf("api_type=json&how=yes&id=%s&uh=%s",
  393.                                 $name,
  394.                                 $this->modHash);
  395.             $response = $this->runCurl($urlEdit, $postData);
  396.         }
  397.         return $response;
  398.     }
  399.    
  400.     public function editThing($name, $text){
  401.         $response = null;
  402.         if ($name && $text){
  403.             $urlEdit = "{$this->apiHost}/editusertext";
  404.             $postData = sprintf("api_type=json&thing_id=%s&text=%s&uh=%s",
  405.                                 $name,
  406.                                 $text,
  407.                                 $this->modHash);
  408.             $response = $this->runCurl($urlEdit, $postData);
  409.         }
  410.         return $response;
  411.     }
  412.    
  413.     /**
  414.     * Vote on a story
  415.     *
  416.     * Adds a vote (up / down / neutral) on a story
  417.     * @link https://github.com/reddit/reddit/wiki/API%3A-vote
  418.     * @param string $name The full name of the post to vote on (name parameter
  419.     *                     in the getSubscriptions() return value)
  420.     * @param int $vote The vote to be made (1 = upvote, 0 = no vote,
  421.     *                  -1 = downvote)
  422.     */
  423.     public function addVote($name, $vote = 1){
  424.         $response = null;
  425.         if ($name){
  426.             $urlVote = "{$this->apiHost}/vote";
  427.             $postData = sprintf("id=%s&dir=%s&uh=%s", $name, $vote, $this->modHash);
  428.             $response = $this->runCurl($urlVote, $postData);
  429.         }
  430.         return $response;
  431.     }
  432.    
  433.     /**
  434.     * Set flair
  435.     *
  436.     * Set or clear a user's flair in a subreddit
  437.     * @link https://github.com/reddit/reddit/wiki/API%3A-flair
  438.     * @param string $subreddit The subreddit to use
  439.     * @param string $user The name of the userLink
  440.     * @param string $text Flair text to assign
  441.     */
  442.     public function setFlair($subreddit, $user, $text, $cssClass){
  443.         $urlFlair = "{$this->apiHost}/flair";
  444.         $postData = sprintf("r=%s&name=%s&text=%s&css_class=%s&uh=%s",
  445.                             $subreddit,
  446.                             $user,
  447.                             $text,
  448.                             $cssClass,
  449.                             $this->modHash);
  450.         $response = $this->runCurl($urlFlair, $postData);
  451.         return $response;
  452.     }
  453.    
  454.     public function setLinkFlair($subreddit, $post, $flair_template_id, $overwrite = false)
  455.     {
  456.         $changeFlair = true;
  457.         if ($overwrite === false) {
  458.             $flairCheck = $this->getRawJSON("by_id/" . $post);
  459.  
  460.             if (!isset($flairCheck->error) && strcmp($flairCheck->kind, "Listing") === 0 && isset($flairCheck->data->children[0])) {            
  461.                 $flairPost = $flairCheck->data->children[0]->data;
  462. // print_r($flairPost);    
  463. // echo "class: " . $flairPost->link_flair_css_class . " " . strcmp($flairPost->link_flair_css_class, "") . chr(10);
  464. // echo "text: " . $flairPost->link_flair_text . " " . strcmp($flairPost->link_flair_text, "") . chr(10);
  465.  
  466.                 if (
  467.                     (strcmp($flairPost->link_flair_css_class, "") !== 0 || strcmp($flairPost->link_flair_text, "") !== 0)
  468.                     && strcmp($flairPost->link_flair_css_class, "curated") !== 0
  469.                     // && strcmp($flairPost->link_flair_text, "curated") !== 0
  470.                 ) $changeFlair = false;
  471.             }
  472.         }
  473.  
  474. // echo "change: " . $changeFlair . chr(10);
  475.         if ($changeFlair) {        
  476.             $urlFlair = "{$this->apiHost}/selectflair";
  477.             $postData = sprintf("r=%s&flair_template_id=%s&link=%s&uh=%s"
  478.                 ,$subreddit
  479.                 ,$flair_template_id
  480.                 ,$post
  481.                 ,$this->modHash
  482.             );
  483.             $response = $this->runCurl($urlFlair, $postData);
  484.             return $response;
  485.         }
  486.         else return '{"skipped":"flair already exists"}';
  487.     }
  488.    
  489.     /**
  490.     * Get flair list
  491.     *
  492.     * Download the flair assignments of a subreddit
  493.     * @link https://github.com/reddit/reddit/wiki/API%3A-flairlist
  494.     * @param string $subreddit The subreddit to use
  495.     * @param int $limit The maximum number of items to return (max 1000)
  496.     * @param string $after Return entries starting after this user
  497.     * @param string $before Return entries starting before this user
  498.     */
  499.     public function getFlairList($subreddit, $limit = 100, $after, $before){
  500.         $urlFlairList = "{$this->apiHost}/share";
  501.         $postData = sprintf("r=%s&limit=%s&after=%s&before=%s&uh=%s",
  502.                             $subreddit,
  503.                             $limit,
  504.                             $after,
  505.                             $before,
  506.                             $this->modHash);
  507.         $response = $this->runCurl($urlFlairList, $postData);
  508.         return $response;
  509.     }
  510.    
  511.     /**
  512.     * Set flair CSV file
  513.     *
  514.     * Post a CSV file of flair settings to a subreddit
  515.     * @link https://github.com/reddit/reddit/wiki/API%3A-flaircsv
  516.     * @param string $subreddit The subreddit to use
  517.     * @param string $flairCSV CSV file contents, up to 100 lines
  518.     */
  519.     public function setFlairCSV($subreddit, $flairCSV){
  520.         $urlFlairCSV = "{$this->apiHost}/flaircsv.json";
  521.         $postData = sprintf("r=%s&flair_csv=%s&uh=%s",
  522.                             $subreddit,
  523.                             $flairCSV,
  524.                             $this->modHash);
  525.         $response = $this->runCurl($urlFlairCSV, $postData);
  526.         return $response;
  527.     }
  528.  
  529. /*
  530. POST [/r/subreddit]/api/wiki/edit
  531. content
  532. page -- the name of an existing page or a new page to create
  533. previous -- the starting point revision for this edit
  534. reason -- a string up to 256 characters long, consisting of printable characters.
  535. uh -- modhash
  536. */
  537.     public function setWikiPage($subreddit, $page, $content) {
  538.         $urlWiki = "{$this->apiHost}/wiki/edit";
  539.         $postData = sprintf("r=%s&uh=%s&page=%s&content=%s",
  540.                             $subreddit
  541.                             ,$this->modHash
  542.                             ,$page
  543.                             ,$content
  544.         );
  545.  
  546.         $response = $this->runCurl($urlWiki, $postData);
  547.         return $response;
  548.     }
  549.  
  550.  
  551.     public function setSubProperty($sr, $prop, $val) {
  552.         $settings = $this->getRawJSON("r/" . $sr . "/about/edit");
  553.  
  554.         if (empty($settings->data)) return false;
  555.         else if ($prop == "link_type") $prop == "content_options";
  556.  
  557.         if (isset($settings->data->{$prop})) $settings->data->{$prop} = $val;
  558.         else { echo "Invalid subreddit property: " . $prop . chr(10); print_r($settings); die(); }
  559.  
  560.         $settings->data->uh = $this->modHash;
  561.         $settings->data->sr = $settings->data->subreddit_id;
  562.         $settings->data->type = $settings->data->subreddit_type;
  563.         $settings->data->link_type = $settings->data->content_options;
  564.  
  565.         foreach ($settings->data as $key => $value)
  566.             if (is_string($value)) $settings->data->{$key} = html_entity_decode($value);
  567.  
  568.         $urlSubmit = "{$this->apiHost}/site_admin";
  569.         $response = $this->runCurl($urlSubmit, $settings->data);
  570.         return $response;
  571.     }
  572.  
  573.  
  574.  
  575. ///////////////////////////////////////
  576.    
  577.     /**
  578.     * cURL request
  579.     *
  580.     * General cURL request function for GET and POST
  581.     * @link URL
  582.     * @param string $url URL to be requested
  583.     * @param string $postVals NVP string to be send with POST request
  584.     */
  585.     private function runCurl($url, $postVals = null, $tryCnt = 1)
  586.     {
  587. // echo "CURL " . $url . chr(10);
  588.         $ch = curl_init($url);
  589.  
  590.         $options = array(
  591.             CURLOPT_RETURNTRANSFER => true
  592.             ,CURLOPT_COOKIE => "reddit_session={$this->session}"
  593.             ,CURLOPT_TIMEOUT => 10
  594.             ,CURLOPT_USERAGENT => 'bot ' . $this->username + ' / radd.it data services by /u/radd_it'
  595.             ,CURLOPT_FAILONERROR => true
  596.         );
  597.        
  598.         if ($postVals != null){
  599.             $options[CURLOPT_POSTFIELDS] = $postVals;
  600.             $options[CURLOPT_CUSTOMREQUEST] = "POST";  
  601.         }
  602.        
  603.         curl_setopt_array($ch, $options);
  604.        
  605.         $response = curl_exec($ch);
  606.         if ($response === false) {
  607.             $toErrIsReddit = curl_error($ch);
  608.             curl_close($ch);
  609.  
  610.             if (strpos($toErrIsReddit, "error: 403") !== false) { $response = new stdClass(); $response->error = "403"; }   // echo "403: " . $url . chr(10);
  611.             else if (strpos($toErrIsReddit, "error: 404") !== false) { $response = new stdClass(); $response->error = "404"; } // echo "404: " . $url . chr(10);
  612.             else if (
  613.                 strcmp(
  614.                     $toErrIsReddit
  615.                     , str_replace(
  616.                         array(
  617.                             "SSL read:", "SSL_write()", " timed out", "error: 502", "error: 503", "error: 504", "error: 520"
  618.                             , "connection timeout", " reset by peer", "Empty reply"
  619.                         )
  620.                         , ""
  621.                         , $toErrIsReddit
  622.                     )
  623.                 ) !== 0
  624.             ) {
  625.                 if ($tryCnt > 2) die(date("M d, Y G:i") . " 3 sequential failures.  Last error: " . $toErrIsReddit);
  626.  
  627.                 // echo "(" . $tryCnt . ") " . $toErrIsReddit . chr(10);
  628.                 sleep(2);
  629.                 $response = $this->runCurl($url, $postVals, $tryCnt + 1);
  630.             }
  631.             else die(date("M d, Y G:i") . " " . $url . " failed, error: " . $toErrIsReddit);
  632.         }
  633.         else {
  634.             $response = json_decode($response);
  635.             curl_close($ch);
  636.             sleep(3);
  637.         }
  638.  
  639.         return $response;
  640.     }
  641. }
  642. ?>
Add Comment
Please, Sign In to add comment