Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * THE BELOW CODE IS BASED ON THE Reddit API BY HENRY SMITH (details below)
- *
- * The main class of the API client, handling its state and the sending of
- * queries to the API
- *
- * @author Henry Smith <henry@henrysmith.org>
- * @copyright 2011 Henry Smith
- * @license GPLv2.0
- * @package Reddit API Client
- * @version 0.5.2
- */
- /**
- * Tries to login to Reddit
- *
- * @access public
- * @return boolean
- */
- public function login($username, $password) {
- $request = new HttpRequest;
- $request->setUrl('http://www.reddit.com/api/login');
- $request->setHttpMethod('POST');
- $request->setPostVariable('user', $username);
- $request->setPostVariable('passwd', $password);
- $request->setHeader('user-agent',$username);
- echo "<pre>";
- print_r($request);
- echo "</pre>";
- $response = $request->getResponse();
- $headers = $response->getHeaders();
- foreach ($headers as $header) {
- if (preg_match('/reddit_session=([^;]+);/', $header, $matches)) {
- $this->sessionCookie = $matches[1];
- return true;
- }
- }
- return false;
- }
- /**
- * Sends a request to Reddit and returns the response received
- *
- * @access public
- * @param string $verb 'GET', 'POST', ...
- * @param string $url 'http://www.reddit.com/comments/6nw57.json'
- * @param string $body
- * @return array
- */
- public function sendRequest($verb, $url, $body = '') {
- $request = new HttpRequest;
- $request->setUrl($url);
- $request->setHttpMethod($verb);
- $request->setHeader('user-agent',$this->username);
- if ($verb === 'POST' && is_array($body)) {
- foreach ($body as $name => $value) {
- $request->setPostVariable($name, $value);
- }
- }
- if ($this->sessionCookie !== null) {
- $request->setCookie('reddit_session', $this->sessionCookie);
- }
- $response = $request->getResponse();
- if (!($response instanceof HttpResponse)) {
- return null;
- }
- $responseBody = $response->getBody();
- $response = json_decode($responseBody, true);
- if (isset($response['data']['modhash'])) {
- $this->modHash = $response['data']['modhash'];
- } elseif (isset($response[0]['data']['modhash'])) {
- $this->modHash = $response[0]['data']['modhash'];
- }
- return $response;
- }
- /**
- * Posts a comment in reply to a link or comment
- *
- * @access public
- * @param string $parentId
- * @param string $text
- * @return boolean
- */
- public function comment($parentId, $text) {
- if (!$this->isLoggedIn()) {
- $message = 'Cannot post a comment without a valid login';
- $code = RedditException::LOGIN_REQUIRED;
- throw new RedditException($message, $code);
- }
- echo $this->modHash;
- $verb = 'POST';
- $url = 'http://www.reddit.com/api/comment';
- $data = array(
- 'thing_id' => $parentId,
- 'text' => $text,
- 'uh' => $this->modHash,
- );
- $response = $this->sendRequest($verb, $url, $data);
- if (!is_array($response) || !isset($response['jquery'])) {
- return false;
- }
- foreach ($response['jquery'] as $element) {
- if (isset($element[3][0][0])) {
- continue;
- }
- if (strpos($element[3][0], '.error') === true) {
- return false;
- }
- }
- return true;
- }
- /**
- * Casts a vote for a comment or link
- *
- * @access public
- * @param string $thingId
- * @param integer $direction 1 for upvote, -1 for down, 0 to remove vote
- * @return boolean
- */
- public function vote($thingId, $direction) {
- if (!$this->isLoggedIn()) {
- $message = 'Cannot vote without a valid login';
- $code = RedditException::LOGIN_REQUIRED;
- throw new RedditException($message, $code);
- }
- $verb = 'POST';
- $url = 'http://www.reddit.com/api/vote';
- $data = array(
- 'thing_id' => $thingId,
- 'dir' => $direction,
- 'uh' => $this->modHash,
- );
- $response = $this->sendRequest($verb, $url, $data);
- if (empty($response)) {
- return true;
- } else {
- return false;
- }
- }
- $rlink = "12f98r"; // The link to the article in question
- print_r($bot0 = new Reddit("botkin0","Redd!@#4"));
- $itsa_link = $bots0->getLink($rlink);
- print_r($itsa_link); // Diagnostic
- echo "<br/><br/>Targeting link $rlink: ".'"<b>'.$itsa_link[title].'"</b>'."<br/>";
- if ($bots0->comment("t3_".$rlink,"Adding third comment"))
- echo "success";
- else
- echo "fail";
- if ($bots0->vote("t3_".$rlink,1))
- echo "success";
- else
- echo "fail";
Add Comment
Please, Sign In to add comment