da404lewzer

Reddit API Class for PHP v0.1 Alpha (Unfinished)

Mar 16th, 2012
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.81 KB | None | 0 0
  1. <?php
  2.  
  3. class Reddit{
  4.  
  5.   var $REDDIT_URL   = "http://www.reddit.com";
  6.   var $LISTING_URL  = "http://www.reddit.com/%s.json?after=%s&limit=%d";  //1st position is subreddit: empty or "r/something/",
  7.                                                                           //2nd position is after_id,
  8.                                                                           //3rd position is result count limit
  9.   var $LOGIN_URL    = "http://www.reddit.com/api/login/%s";               //1st position is username (redundant and required)
  10.   var $VOTE_URL     = "http://www.reddit.com/api/vote";
  11.  
  12.   var $username = "guest"; //todo: handle this better
  13.   var $modhash = "";
  14.   var $cookie = "";
  15.  
  16.   function getDataAsJson($url){
  17.     return json_decode(file_get_contents($url));
  18.   }
  19.  
  20.   function getListings($subreddit="", $after=""){
  21.     if (trim($subreddit) != "" && substr($subreddit, -1) != "/"){
  22.       $subreddit .= "/";
  23.     }
  24.     return $this->getDataAsJson(sprintf($this->LISTING_URL, $subreddit, $after, 100));
  25.   }
  26.  
  27.   function postData($url, $data){
  28.     $c = curl_init($url);
  29.     curl_setopt($c, CURLOPT_HEADER, 0);    
  30.     curl_setopt($c, CURLOPT_POST, true);
  31.     curl_setopt($c, CURLOPT_POSTFIELDS, $data);
  32.     curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
  33.     curl_setopt($c, CURLOPT_COOKIEJAR, sprintf("cookies/%s.txt", $this->username));
  34.     curl_setopt($c, CURLOPT_COOKIEFILE, sprintf("cookies/%s.txt", $this->username));
  35.     curl_setopt($c, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
  36.     $data = curl_exec($c);
  37.     curl_close($c);
  38.     $tmp_data = json_decode($data);
  39.     if ($tmp_data != NULL){
  40.       $this->modhash = $tmp_data->json->data->modhash;
  41.       $this->cookie = $tmp_data->json->data->cookie;
  42.     }
  43.     return $data;
  44.   }
  45.  
  46.   function makeBodyString($data){
  47.     //PHP 5 or better, I will write a 4.0 version later.
  48.     return http_build_query($data);
  49.   }
  50.  
  51.   function login($username, $password){
  52.     $this->username = $username;
  53.     $data = $this->makeBodyString(array(
  54.                                           "user"=>$username,    //user's username
  55.                                           "passwd"=>$password,  //user's password
  56.                                           "api_type"=>"json"    //required
  57.                                         ));
  58.     return $this->postData(sprintf($this->LOGIN_URL, $username), $data);
  59.   }
  60.  
  61.   function submitVote($id, $vote=0){
  62.     $data = $this->makeBodyString(array(
  63.                                           "id"=>$id,            //article id
  64.                                           "dir"=>$vote,         //vote direction. 1=up, 0=none, -1=down
  65.                                           "uh"=>$this->modhash  //modhash
  66.                                         ));
  67.     return $this->postData($this->VOTE_URL, $data);
  68.   }
  69. }
Add Comment
Please, Sign In to add comment