Advertisement
Guest User

Untitled

a guest
Jan 13th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.16 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Simple method which performs a login operation to Polish reddit-like "wykop.pl"
  4.  * and fetches the number of notifications.
  5.  *
  6.  * @todo This is made just for fun! My activity on the websites was too low to
  7.  * be granted access to Wykop API.
  8.  *
  9.  * @param string $operation token, login, notifs
  10.  * @param string $username
  11.  * @param string $passwod
  12.  * @return int|string notifs count, token
  13.  */
  14. function wykopnotifs($operation = 'token', $username = null, $password = null)
  15. {
  16.     //start curl
  17.     $ch =  curl_init();
  18.     $useragent = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2';
  19.  
  20.     switch ($operation) {
  21.         case 'notifs':
  22.             $url = 'https://www.wykop.pl/powiadomienia/do-mnie/';
  23.             break;
  24.         case 'login':
  25.             $token = wykopnotifs('token');
  26.             curl_setopt( $ch, CURLOPT_POST, true );
  27.             curl_setopt( $ch, CURLOPT_POSTFIELDS, 'user[username]=' . $username . '&user[password]=' . $password . '&__token=' . $token . '&submit=login' );
  28.         case 'token':
  29.             $url = 'https://www.wykop.pl/zaloguj/';
  30.             break;
  31.     }
  32.  
  33.     curl_setopt( $ch, CURLOPT_URL, $url );
  34.     curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
  35.     curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
  36.     curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
  37.     curl_setopt( $ch, CURLOPT_REFERER, 'https://www.wykop.pl/zaloguj/' );
  38.     curl_setopt( $ch, CURLOPT_USERAGENT, $useragent );
  39.     curl_setopt( $ch, CURLOPT_COOKIEJAR,  'cookie.txt' );
  40.     curl_setopt( $ch, CURLOPT_COOKIEFILE, 'cookie.txt' );
  41.     $result = curl_exec( $ch );
  42.     curl_close( $ch );
  43.  
  44.     switch ($operation) {
  45.         case 'notifs':
  46.             if (stripos($result, "newregister")) {
  47.                 wykopnotifs('login', $username, $password);
  48.             } else {
  49.                 return substr_count($result, "type-light-warning");
  50.             }
  51.         case 'token':
  52.             $matches = [];
  53.             preg_match("/name=\"__token\" value=\"(.+)?\"/i", $result, $matches);
  54.             return $matches[1][0];
  55.     }
  56.  
  57.     return $result;
  58. }
  59.  
  60. //echo wykopnotifs('notifs', '**login**', '**password**');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement