Advertisement
Guest User

Twitter Login with cURL

a guest
Feb 21st, 2018
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.84 KB | None | 0 0
  1. <?php
  2.  
  3. require_once dirname(__DIR__) . '/settings/Settings.php';
  4.  
  5. function login($username, $password)
  6. {
  7.     $cookie = '';
  8.     $location = '';
  9.  
  10.     $request = curl_init();
  11.     curl_setopt_array($request, array(
  12.         CURLOPT_URL                         => 'https://twitter.com',
  13.         CURLOPT_CUSTOMREQUEST       => 'GET',
  14.     CURLOPT_RETURNTRANSFER  => true,
  15.     CURLOPT_SSL_VERIFYPEER  => false,
  16.     CURLOPT_SSL_VERIFYHOST  => false,
  17.     CURLOPT_HEADER                  => true,
  18.     CURLOPT_USERAGENT => $_SERVER['HTTP_USER_AGENT'],
  19.     CURLOPT_COOKIEJAR               => getcwd() . '/cookies/' . $username . '.txt',
  20.     CURLOPT_HEADERFUNCTION  => function($curl, $header) use (&$cookie)
  21.     {
  22.         if (stripos($header, 'Set-Cookie:') === 0)
  23.         {
  24.             if (preg_match('/^Set-Cookie: \s*([^;]*)/i', $header, $matches))
  25.             {
  26.                 $cookie .= $matches[1] . '; ';
  27.             }
  28.         }
  29.         return strlen($header);
  30.     }
  31.     ));
  32.     $response = curl_exec($request);
  33.  
  34.     preg_match('/value="(.*?)" name="authenticity_token"/', $response, $matches);
  35.  
  36.     $authenticity_token = $matches[1];
  37.  
  38.     $post = http_build_query(array(
  39.         'session' => array(
  40.             'username_or_email' => $username,
  41.             'password'                  => $password
  42.         ),
  43.         'return_to_ssl'                 => true,
  44.         'scribe_log'                        => '',
  45.         'redirect_after_login'  => '/',
  46.         'authenticity_token'        => $authenticity_token,
  47.         'remember_me'                       => 1
  48.     ));
  49.  
  50.     curl_setopt_array($request, array(
  51.         CURLOPT_URL                         => 'https://twitter.com/sessions',
  52.         CURLOPT_CUSTOMREQUEST   => 'POST',
  53.         CURLOPT_POSTFIELDS          => $post,
  54.         CURLOPT_RETURNTRANSFER  => true,
  55.         CURLOPT_SSL_VERIFYPEER  => false,
  56.         CURLOPT_SSL_VERIFYHOST  => false,
  57.         CURLOPT_COOKIE                  => $cookie,
  58.         CURLOPT_USERAGENT => $_SERVER['HTTP_USER_AGENT'],
  59.         CURLOPT_HTTPHEADER          => array(
  60.             'accept-language: pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4',
  61.       'content-type: application/x-www-form-urlencoded',
  62.       'origin: https://twitter.com',
  63.       'referer: https://twitter.com/login',
  64.         ),
  65.         CURLOPT_HEADERFUNCTION  => function($curl, $header) use (&$cookie, &$location)
  66.         {
  67.             if (stripos($header, 'Set-Cookie:') === 0)
  68.             {
  69.                 if (preg_match('/^Set-Cookie: \s*([^;]*)/i', $header, $matches))
  70.                 {
  71.                     $cookie .= $matches[1] . '; ';
  72.                 }
  73.             }
  74.  
  75.             if (stripos($header, 'Location:') === 0)
  76.             {
  77.                 $location = trim(str_ireplace('location: ', '', trim($header)));
  78.             }
  79.             return strlen($header);
  80.         }
  81.     ));
  82.     $response = curl_exec($request);
  83.     $_SESSION['login_cookies'] = $cookie;
  84.  
  85.     $cookie_array = explode('; ', $cookie);
  86.  
  87.     $_SESSION['cookie_array'] = $cookie_array;
  88.  
  89.     curl_close($request);
  90.  
  91.     $location = trim(explode('?', $location)[0], '/');
  92.  
  93.     switch ($location)
  94.     {
  95.         case 'https://twitter.com':
  96.             echo json_encode(array(
  97.                 'success' => true,
  98.                 'message' => 'Logado com sucesso aguarde...'
  99.             ));
  100.             break;
  101.  
  102.             case 'https://twitter.com/account/access':
  103.                 echo json_encode(array(
  104.                     'success' => false,
  105.                     'message' => 'Sua conta está bloqueada entre no Twitter para desbloquea-la'
  106.                 ));
  107.                 break;
  108.  
  109.             case 'https://twitter.com/account/login_verification':
  110.                 echo json_encode(array(
  111.                     'success' => false,
  112.                     'message' => 'Sua conta exige verificação via 2FA'
  113.                 ));
  114.                 break;
  115.        
  116.         default:
  117.             echo json_encode(array(
  118.                 'success' => false,
  119.                 'message' => 'Usuário e/ou Senha incorretos'
  120.             ));
  121.             break;
  122.     }
  123. }
  124.  
  125. if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'POST')
  126. {
  127.     $username = strip_tags(trim(filter_input(INPUT_POST, 'username')));
  128.     $password = strip_tags(trim(filter_input(INPUT_POST, 'password')));
  129.    
  130.     if (empty($username))
  131.     {
  132.         echo json_encode(array(
  133.             'success' => false,
  134.             'message' => 'Informe seu usuário para continuar.'
  135.         ));
  136.     }
  137.     elseif (empty($password))
  138.     {
  139.         echo json_encode(array(
  140.             'success' => false,
  141.             'message' => 'Informe sua senha para continuar.'
  142.         ));
  143.     }
  144.     else
  145.     {
  146.         login($username, $password);
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement