php curl simulates login and obtains web page content

WBOY
Release: 2016-07-25 08:53:08
Original
1227 people have browsed it
  1. class CURL

  2. {
  3. var $cookie_file; //Set the cookie file saving path and file name
  4. var $loginurl; //Login address
  5. var $actionstr; / /Login parameters
  6. function __construct()
  7. {
  8. $this->cookie_file=tempnam("./TEMP","COOKIedsdsdsdsdsdwerwdszx454.txt");
  9. }

  10. function vlogin()

  11. { //Simulated login by bbs.it-home.org
  12. $ch = curl_init();
  13. curl_setopt($ch, CURLOPT_URL,$this->loginurl);
  14. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);// Whether to display data 0 means displaying 1 or not displaying
  15. curl_setopt($ch, CURLOPT_POST, 1);
  16. curl_setopt($ch, CURLOPT_POSTFIELDS, $this->actionstr);
  17. curl_setopt($ch,CURLOPT_COOKIEJAR,$this-> cookie_file);
  18. $data = curl_exec($ch);
  19. curl_close($ch);
  20. }

  21. function gethtml($url)

  22. { //Get web page content php simulated login
  23. $ curl = curl_init(); // Start a CURL session
  24. curl_setopt($curl, CURLOPT_URL, $url); // The address to be accessed
  25. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // Check the source of the authentication certificate
  26. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); // Check whether the SSL encryption algorithm exists from the certificate
  27. curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // Simulate the browser used by the user
  28. curl_setopt($ curl, CURLOPT_FOLLOWLOCATION, 1); // Use automatic redirect
  29. curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // Automatically set Referer
  30. curl_setopt($curl, CURLOPT_HTTPGET, 1); // Send a regular Post request
  31. curl_setopt ($curl, CURLOPT_COOKIEFILE, $this->cookie_file); // Read the cookie information stored above
  32. curl_setopt($curl, CURLOPT_TIMEOUT, 30); // Set timeout limit to prevent infinite loop
  33. curl_setopt($curl, CURLOPT_HEADER , 0); // Display the returned Header area content
  34. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // The obtained information is returned in the form of a file stream
  35. $tmpInfo = curl_exec($curl); // Execute the operation
  36. if (curl_errno($curl)) {
  37. echo 'Errno'.curl_error($curl);
  38. }
  39. curl_close($curl); // Close CURL session
  40. return $tmpInfo; // Return data
  41. }
  42. }
  43. $ host="http://192.168.0.2";
  44. $mycurl=new CURL();
  45. $mycurl->actionstr="user_name=111&uspassword=111";
  46. $mycurl->loginurl=$host."/ admin/login.php";
  47. $mycurl->vlogin();//Log in
  48. $htmlcontent=$mycurl->gethtml($host."/user.php?id=123");//Get this The content of the page
  49. echo $htmlcontent;//Display the content of this page
  50. //Successfully obtained

Copy code

>>> More php simulation login articles, topic link: php simulation Log in to php curl simulated login tutorial



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template