Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* php curl function to enable cookie without cookiejar (I got it from github) */function fetch($url, $cookies = null, $returnCookie = false) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); if($cookies){ curl_setopt($ch, CURLOPT_COOKIE, implode(';',$cookies)); } curl_setopt($ch, CURLOPT_HEADER, 1); $result = curl_exec($ch); list($header, $body) = explode("\r\n\r\n", $result, 2); $end = strpos($header, 'Content-Type'); $start = strpos($header, 'Set-Cookie'); $parts = explode('Set-Cookie:', substr($header, $start, $end - $start)); $cookies = array(); foreach ($parts as $co) { $cd = explode(';', $co); if (!empty($cd[0])) $cookies[] = $cd[0]; } curl_close($ch); if ($returnCookie){ return $cookies; } return json_decode($body);}
- /** * * my code starts here * */
- /* request new session on twitter login page */$request1 = curl_init();curl_setopt_array($request1, Array( CURLOPT_URL => 'hxx ps: / /mobile . twitter . c*m /session /new', CURLOPT_FOLLOWLOCATION => false, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_RETURNTRANSFER => true, CURLOPT_COOKIEJAR => 'cookies.txt', CURLOPT_TIMEOUT => 13, CURLOPT_USERAGENT => "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 Safari/537.36", CURLOPT_HEADER => true));$response1 = curl_exec($request1);curl_close($request1);
- /* get twitter authenticity_token */preg_match("/authenticity_token\" type=\"hidden\" value=\"([^\"]+)/i", $response1, $token);$mytoken = $token[1];
- /* data to be posted */$user = 'USERNAME';$pass = 'PASSWORD';$postdata = "authenticity_token=" . rawurlencode($mytoken) . "&username=" . $user . "&password=" . $pass;
- /* next request which needs cookiefile */$request2 = curl_init();curl_setopt_array($request2, Array( CURLOPT_URL => 'hxx ps: / /mobile . twitter . c*m /session', CURLOPT_SSL_VERIFYPEER => false, CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => false, CURLOPT_COOKIEJAR => 'cookies.txt', CURLOPT_COOKIEFILE => 'cookies.txt', CURLOPT_TIMEOUT => 13, CURLOPT_USERAGENT => "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 Safari/537.36", CURLOPT_HEADER => true, CURLOPT_REFERER => 'hxx ps: / /mobile . twitter . c*m /session /new', CURLOPT_CUSTOMREQUEST => "POST", CURLOPT_POST => true, CURLOPT_POSTFIELDS => $postdata));$response2 = curl_exec($request2);curl_close($request2);
- /* save response to txt file, for learning purpose */$check = fopen('response.txt', 'w+');fwrite($check, $response2);fclose($check);
Add Comment
Please, Sign In to add comment