Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

php reddit API test login, comment, and vote

By: a guest on Nov 1st, 2012  |  syntax: PHP  |  size: 5.03 KB  |  views: 137  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /**
  2.  * THE BELOW CODE IS BASED ON THE Reddit API BY HENRY SMITH (details below)
  3.  *
  4.  * The main class of the API client, handling its state and the sending of
  5.  * queries to the API
  6.  *
  7.  * @author    Henry Smith <henry@henrysmith.org>
  8.  * @copyright 2011 Henry Smith
  9.  * @license   GPLv2.0
  10.  * @package   Reddit API Client
  11.  * @version   0.5.2
  12.  */
  13.  
  14.         /**
  15.         * Tries to login to Reddit
  16.         *
  17.         * @access public
  18.         * @return boolean
  19.         */
  20.         public function login($username, $password) {
  21.         $request = new HttpRequest;
  22.         $request->setUrl('http://www.reddit.com/api/login');
  23.         $request->setHttpMethod('POST');
  24.         $request->setPostVariable('user', $username);
  25.         $request->setPostVariable('passwd', $password);
  26.                 $request->setHeader('user-agent',$username);
  27.                
  28.                 echo "<pre>";
  29.                 print_r($request);
  30.                 echo "</pre>";
  31.  
  32.         $response = $request->getResponse();
  33.         $headers = $response->getHeaders();
  34.  
  35.  
  36.         foreach ($headers as $header) {
  37.             if (preg_match('/reddit_session=([^;]+);/', $header, $matches)) {
  38.                 $this->sessionCookie = $matches[1];
  39.                 return true;
  40.             }
  41.         }
  42.  
  43.         return false;
  44.     }
  45.        
  46.  
  47.     /**
  48.      * Sends a request to Reddit and returns the response received
  49.      *
  50.      * @access public
  51.      * @param  string $verb  'GET', 'POST', ...
  52.      * @param  string $url   'http://www.reddit.com/comments/6nw57.json'
  53.      * @param  string $body
  54.      * @return array
  55.      */
  56.         public function sendRequest($verb, $url, $body = '') {
  57.         $request = new HttpRequest;
  58.         $request->setUrl($url);
  59.         $request->setHttpMethod($verb);
  60.                 $request->setHeader('user-agent',$this->username);
  61.  
  62.         if ($verb === 'POST' && is_array($body)) {
  63.             foreach ($body as $name => $value) {
  64.                 $request->setPostVariable($name, $value);
  65.             }
  66.         }
  67.  
  68.         if ($this->sessionCookie !== null) {
  69.             $request->setCookie('reddit_session', $this->sessionCookie);
  70.         }
  71.  
  72.         $response = $request->getResponse();
  73.  
  74.         if (!($response instanceof HttpResponse)) {
  75.             return null;
  76.         }
  77.  
  78.         $responseBody = $response->getBody();
  79.         $response = json_decode($responseBody, true);
  80.  
  81.         if (isset($response['data']['modhash'])) {
  82.             $this->modHash = $response['data']['modhash'];
  83.         } elseif (isset($response[0]['data']['modhash'])) {
  84.             $this->modHash = $response[0]['data']['modhash'];
  85.         }
  86.  
  87.         return $response;
  88.     }
  89.  
  90.     /**
  91.      * Posts a comment in reply to a link or comment
  92.      *
  93.      * @access public
  94.      * @param  string $parentId
  95.      * @param  string $text
  96.      * @return boolean
  97.      */
  98.     public function comment($parentId, $text) {
  99.         if (!$this->isLoggedIn()) {
  100.             $message = 'Cannot post a comment without a valid login';
  101.             $code = RedditException::LOGIN_REQUIRED;
  102.             throw new RedditException($message, $code);
  103.         }
  104.                 echo $this->modHash;
  105.  
  106.         $verb = 'POST';
  107.         $url = 'http://www.reddit.com/api/comment';
  108.         $data = array(
  109.             'thing_id' => $parentId,
  110.             'text' => $text,
  111.             'uh' => $this->modHash,
  112.         );
  113.  
  114.         $response = $this->sendRequest($verb, $url, $data);
  115.  
  116.         if (!is_array($response) || !isset($response['jquery'])) {
  117.             return false;
  118.         }
  119.  
  120.         foreach ($response['jquery'] as $element) {
  121.  
  122.             if (isset($element[3][0][0])) {
  123.                 continue;
  124.             }
  125.  
  126.             if (strpos($element[3][0], '.error') === true) {
  127.                 return false;
  128.             }
  129.         }
  130.  
  131.  
  132.         return true;
  133.     }
  134.  
  135.     /**
  136.      * Casts a vote for a comment or link
  137.      *
  138.      * @access public
  139.      * @param  string  $thingId
  140.      * @param  integer $direction  1 for upvote, -1 for down, 0 to remove vote
  141.      * @return boolean
  142.      */
  143.     public function vote($thingId, $direction) {
  144.         if (!$this->isLoggedIn()) {
  145.             $message = 'Cannot vote without a valid login';
  146.             $code = RedditException::LOGIN_REQUIRED;
  147.             throw new RedditException($message, $code);
  148.         }
  149.  
  150.         $verb = 'POST';
  151.         $url = 'http://www.reddit.com/api/vote';
  152.         $data = array(
  153.             'thing_id' => $thingId,
  154.             'dir' => $direction,
  155.             'uh' => $this->modHash,
  156.         );
  157.  
  158.         $response = $this->sendRequest($verb, $url, $data);
  159.  
  160.         if (empty($response)) {
  161.             return true;
  162.         } else {
  163.             return false;
  164.         }
  165.     }  
  166.  
  167.         $rlink = "12f98r";                                      // The link to the article in question
  168.  
  169.         print_r($bot0 = new Reddit("botkin0","Redd!@#4"));
  170.         $itsa_link = $bots0->getLink($rlink);
  171.        
  172.         print_r($itsa_link);            // Diagnostic
  173.        
  174.         echo "<br/><br/>Targeting link $rlink: ".'"<b>'.$itsa_link[title].'"</b>'."<br/>";
  175.        
  176.         if ($bots0->comment("t3_".$rlink,"Adding third comment"))
  177.         echo "success";
  178.         else
  179.         echo "fail";
  180.         if ($bots0->vote("t3_".$rlink,1))
  181.         echo "success";
  182.         else
  183.         echo "fail";