Skip to content

Instantly share code, notes, and snippets.

@ryaan-anthony
Created May 31, 2015 15:13
Show Gist options
  • Select an option

  • Save ryaan-anthony/b2ff8130c5240dd1ee1d to your computer and use it in GitHub Desktop.

Select an option

Save ryaan-anthony/b2ff8130c5240dd1ee1d to your computer and use it in GitHub Desktop.
<?php
define( 'REDDIT_API_USER_HASH', 'reddit_api' );
class Reddit_Api_Bot{
//@mixed Set after successful login, otherwise false
public $user_hash;
//@mixed Last callback result
public $last_result;
//@array Last recorded error
public $last_error;
//@string Change your client's User-Agent string to something unique and descriptive
private static $user_agent;
//@string Save session ID that we can now pass to other parts of the reddit API
private static $cookie_path;
//@array Reddit Api callback array
private static $the_callbacks;
/**
* set up the basics
*/
function __construct( $args, $force_login = false ){
$this->user_hash = $this->user_session( $args, $force_login );
$this->user_agent = 'Reddit API example bot #' . md5( __DIR__ ) . ' from ' . $_SERVER['HTTP_USER_AGENT'];
$this->cookie_path = __DIR__ . '/cookie.txt';
return $this;
}
/**
* unique identifier for the session
*/
private static function unique_user(){
foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED',
'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key) {
if (array_key_exists($key, $_SERVER) === true) {
foreach (explode(',', $_SERVER[$key]) as $ip) {
if (filter_var($ip, FILTER_VALIDATE_IP) !== false) {
return md5( REDDIT_API_USER_HASH.'_'.str_replace(".","_",$ip) );
}
}
}
}
}
/**
* remember user hash
*/
public function user_session( $args, $force_login ){
if(!$force_login && isset($_COOKIE[$this->unique_user()])){
return stripslashes($_COOKIE[$this->unique_user()]);
}
if( $login = $this->do_action( 'login', $args ) ){
setcookie(
$this->unique_user(),
addslashes( $login ),
time()+31556952,
'/'
);
}
return $login;
}
/**
* connect to reddit api
* actions: login, submit, del, compose, comment, editusertext
* for args see: https://github.com/reddit/reddit/wiki/API
*
*/
public function do_action( $action, $args ){
$url = "http://www.reddit.com/api/" . ($action == "captcha" ? "submit" : $action);
$args["api_type"] = 'json';
$ch = curl_init( );
curl_setopt( $ch, CURLOPT_POST, 1 );
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query($args) );
curl_setopt( $ch, CURLOPT_USERAGENT, $this->user_agent );
curl_setopt( $ch, CURLOPT_COOKIEJAR, $this->cookie_path );
curl_setopt( $ch, CURLOPT_COOKIEFILE, $this->cookie_path );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
$result = json_decode( curl_exec( $ch ) );
curl_close( $ch );
return $this->do_callback( $action, $result );
}
/**
* validate the result
*/
private function do_callback( $action, $result ){
$bot = $this;
$the_callbacks = array(
//returns user hash, else false
'login' => function() use ( $result, $bot ){
if( $error = $result->json->errors[0][1] ){
$bot->last_error = $error;
} elseif( $uh = $result->json->data->modhash ){
$bot->user_hash = $uh;
return $uh;
} else {
$bot->last_error = 'login failed';
}
return false;
},
//returns html image and input, else false
'captcha' => function() use ( $result ){
if( $captcha = $result->jquery[10][3][0] ){
return "<img src='http://reddit.com/captcha/{$captcha}.png'>
<input type='hidden' name='captcha_id' value='{$captcha}' />
<input placeholder='are you human? (sorry)' name='captcha_answer' />";
}
return false;
},
//returns ...
'submit' => function() use ( $result ){
if( $error = $result->json->errors[0][1] ){
return $error;
} else {
print_r($result);
}
return false;
},
'del' => function() use ( $result, $bot ){
},
'compose' => function() use ( $result, $bot ){
},
'comment' => function() use ( $result, $bot ){
},
'editusertext' => function() use ( $result, $bot ){
}
);
return $this->last_result = call_user_func($the_callbacks[ $action ]);
}
/**
* get the captcha
*/
public function get_captcha(){
return $this->do_action(
'captcha',
array(
"kind" => "self",
"text" => "captcha",
"sr" => "test",
"title" => "",
"uh" => urlencode($this->user_hash)
)
);
}
/**
* format the captcha response
*/
public function captcha_reponse( ){
if(!isset($_POST['captcha_id'])){return array();}
return array( 'captcha' => $_POST['captcha_answer'], 'iden' => $_POST['captcha_id'] );
}
/**
* submit self post
* @array args: title, text, sr
*/
public function self_post( $args ){
$args = array_merge( $args, array(
"kind" => "self",
"uh" => $this->user_hash
));
return $this->do_action( 'submit', $args );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment