Advertisement
Guest User

2

a guest
Feb 12th, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.28 KB | None | 0 0
  1. <?php
  2. $url = 'urlogin';
  3. $ckfile = './ctemp/cookie.txt';
  4. $useragent = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.2 (KHTML, like Gecko) Chrome/5.0.342.3 Sa
  5. fari/533.2';
  6.  
  7. $username = "user";
  8. $password = "pass";
  9.  
  10.  
  11. $f = fopen('log.txt', 'w'); // file to write request header for debug purpose
  12.  
  13. /**
  14.     Get __VIEWSTATE & __EVENTVALIDATION
  15.  */
  16. $ch = curl_init($url);
  17. curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile);
  18. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  19. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  20. curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
  21.  
  22. $html = curl_exec($ch);
  23.  
  24. curl_close($ch);
  25.  
  26. preg_match('~<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="(.*?)" />~', $html, $viewstate);
  27. preg_match('~<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="(.*?)" />~', $html, $eventVali
  28. dation);
  29.  
  30. $viewstate = $viewstate[1];
  31. $eventValidation = $eventValidation[1];
  32.  
  33.  
  34.  
  35. /**
  36.  Start Login process
  37.  */
  38. $ch = curl_init();
  39.  
  40. curl_setopt($ch, CURLOPT_URL, $url);
  41. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  42. curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
  43. curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile);
  44. curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);
  45. curl_setopt($ch, CURLOPT_HEADER, FALSE);
  46. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  47. curl_setopt($ch, CURLOPT_REFERER, $url);
  48. curl_setopt($ch, CURLOPT_VERBOSE, 1);
  49. curl_setopt($ch, CURLOPT_STDERR, $f);
  50. curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
  51.  
  52. // Collecting all POST fields
  53. $postfields = array();
  54. $postfields['__EVENTTARGET'] = 'ctl00$ContentPlaceHolder1$CtrlLogin1$btnValidar';
  55. $postfields['__EVENTARGUMENT'] = "";
  56. $postfields['__VIEWSTATE'] = $viewstate;
  57. $postfields['__EVENTVALIDATION'] = $eventValidation;
  58. $postfields['ctl00$ContentPlaceHolder1$CtrlLogin1$tbNifEmail'] = $username;
  59. $postfields['ctl00$ContentPlaceHolder1$CtrlLogin1$tbPassword'] = $password;
  60. $postfields['ctl00$ContentPlaceHolder1$hdnSelectedPubli'] = '';
  61.  
  62. curl_setopt($ch, CURLOPT_POST, 1);
  63. curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
  64. $ret = curl_exec($ch); // Get result after login page.
  65.  
  66. print $ret;
  67.  
  68. // Recoge datos de la cuenta
  69.  
  70. $url = 'urlarecoger';
  71. curl_setopt($ch, CURLOPT_URL, $url);
  72. curl_setopt($ch, CURLOPT_POST, 0);
  73.  
  74. $data = curl_exec($ch);
  75. print $data;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement