Skip to content

Instantly share code, notes, and snippets.

@skorotkiewicz
Created December 8, 2016 15:20
Show Gist options
  • Save skorotkiewicz/079b3d2f752c115e7332ec7a01a47392 to your computer and use it in GitHub Desktop.
Save skorotkiewicz/079b3d2f752c115e7332ec7a01a47392 to your computer and use it in GitHub Desktop.
Ask.fm ask question bot
<?PHP
/*
* Ask.fm ask question bot, ver 0.1
* Bot can automatically login to your account and ask questions
* required: Questions e.g. from mysql and nicks to ask
* */
define('SETUSERAGENT', 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)');
function echow($text) {
if (isset($_SERVER{'TERM'})) {
echo $text . "\t\n";
} else {
echo $text . "<br />";
}
ob_flush();
flush();
ob_end_flush();
ob_start();
}
function getToken() {
$a = curl_init("http://ask.fm/pytajowiec");
curl_setopt($a, CURLOPT_USERAGENT, SETUSERAGENT);
curl_setopt($a, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($a, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($a, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($a, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($a, CURLOPT_MAXREDIRS, 10);
curl_setopt($a, CURLOPT_REFERER, 'http://ask.fm/');
curl_setopt($a, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($a, CURLOPT_HEADER, FALSE);
curl_setopt($a, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($a, CURLOPT_SSL_VERIFYHOST, 2);
$wynik = curl_exec($a);
curl_close($a);
preg_match_all('#<input name="authenticity_token" type="hidden" value="(.*)" />#', $wynik, $token);
return $token[1][0];
}
function login($login, $password) {
$token = getToken();
$curlchanel = curl_init("http://ask.fm/session");
curl_setopt($curlchanel, CURLOPT_USERAGENT, SETUSERAGENT);
curl_setopt($curlchanel, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($curlchanel, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($curlchanel, CURLOPT_HEADER, 0);
curl_setopt($curlchanel, CURLOPT_TIMEOUT, 6);
curl_setopt($curlchanel, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($curlchanel, CURLOPT_POSTFIELDS, trim("authenticity_token=$token&login=$login&password=$password&follow=&like=&back=&authenticity_token=$token"));
$wynik = curl_exec($curlchanel);
if ($wynik != 1) {
echow("Unable to login");
die;
}
curl_close($curlchanel);
}
function ask($question, $user) {
$token = getToken();
$curlchanel = curl_init("http://ask.fm/$user/questions/create");
curl_setopt($curlchanel, CURLOPT_USERAGENT, SETUSERAGENT);
curl_setopt($curlchanel, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($curlchanel, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($curlchanel, CURLOPT_HEADER, 0);
curl_setopt($curlchanel, CURLOPT_TIMEOUT, 6);
curl_setopt($curlchanel, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($curlchanel, CURLOPT_POSTFIELDS, trim("authenticity_token=$token&question%5Bquestion_text%5D=$question%3F&question%5Bforce_anonymous%5D=&authenticity_token=$token"));
$wynik = curl_exec($curlchanel);
if ($wynik != 1) {
echow("Unable to login");
die;
}
curl_close($curlchanel);
}
login('YOUR LOGIN', 'YOUR PASSWORD');
ask('YOUR QUESTION', 'LOGIN');
/*
*
* //example usage with mysql
* $sql = $db->query("SELECT * FROM `questions` LIMIT 40, 3726 ");
* foreach ($sql as $row) {
* ask($row['question'], 'labudson'); // post it!
* sleep(3);
* echo $row['question']."\n";
* echo " === QUESTION ID [".$row['id']."/200] === ";
* }
*
* */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment