PHP code accessed through curl login

Source: Internet
Author: User
PHP code accessed through curl login

  1. $ Data = 'username = zjzhoufy@126.com & password = 1q2w3e & remember = 1 ';
  2. $ Curlobj = curl_init (); // Initialization
  3. Curl_setopt ($ curlobj, CURLOPT_URL, "http://www.imooc.com/user/login"); // sets the URL for accessing the web page
  4. Curl_setopt ($ curlobj, CURLOPT_RETURNTRANSFER, true); // It is not printed directly after execution.
  5. // Cookie settings. these settings must be set before all sessions start.
  6. Date_default_timezone_set ('prc'); // you must set the time zone before using cookies.
  7. Curl_setopt ($ curlobj, CURLOPT_COOKIESESSION, TRUE );
  8. Curl_setopt ($ curlobj, CURLOPT_COOKIEJAR, 'cookie.txt '); // Save
  9. Curl_setopt ($ curlobj, CURLOPT_COOKIEFILE, 'cookie.txt '); // Read
  10. Curl_setopt ($ curlobj, CURLOPT_HEADER, 0 );
  11. Curl_setopt ($ curlobj, CURLOPT_FOLLOWLOCATION, 1); // This allows cURL to support page link jump
  12. Curl_setopt ($ curlobj, CURLOPT_COOKIE, session_name (). '='. session_id ());
  13. Curl_setopt ($ curlobj, CURLOPT_POST, 1 );
  14. Curl_setopt ($ curlobj, CURLOPT_POSTFIELDS, $ data );
  15. Curl_setopt ($ curlobj, CURLOPT_HTTPHEADER, array ("application/x-www-form-urlencoded; charset = utf-8 ",
  16. "Content-length:". strlen ($ data)
  17. ));
  18. Curl_exec ($ curlobj); // execute
  19. Curl_setopt ($ curlobj, CURLOPT_URL, "http://www.imooc.com/space/index ");
  20. Curl_setopt ($ curlobj, CURLOPT_POST, 0 );
  21. Curl_setopt ($ curlobj, CURLOPT_HTTPHEADER, array ("Content-type: text/xml"
  22. ));
  23. $ Output = curl_exec ($ curlobj); // execute
  24. Curl_close ($ curlobj); // close cURL
  25. Echo $ output;
  26. ?>


The above code is not perfect. we should store the cookie in the cache, and it should not be persistent. In addition, such persistence can only be accessed by a single user.

Therefore, we only need to set

  1. CURLOPT_COOKIESESSION

As follows:

  1. $ Data = 'username = demo_demo@126.com & password = 123456qwe & remember = 1 ';
  2. $ Curlobj = curl_init (); // Initialization
  3. Curl_setopt ($ curlobj, CURLOPT_URL, "http://www.imooc.com/user/login"); // sets the URL for accessing the web page
  4. Curl_setopt ($ curlobj, CURLOPT_RETURNTRANSFER, true); // It is not printed directly after execution.
  5. // Cookie settings. these settings must be set before all sessions start.
  6. Date_default_timezone_set ('prc'); // you must set the time zone before using cookies.
  7. Curl_setopt ($ curlobj, CURLOPT_COOKIESESSION, TRUE );
  8. Curl_setopt ($ curlobj, CURLOPT_HEADER, 0 );
  9. Curl_setopt ($ curlobj, CURLOPT_COOKIE, session_name (). '='. session_id ());
  10. // Comment out this line because the security mode and open_basedir must be disabled for this setting, which is not conducive to server security
  11. // Curl_setopt ($ curlobj, CURLOPT_FOLLOWLOCATION, 1 );
  12. Curl_setopt ($ curlobj, CURLOPT_POST, 1 );
  13. Curl_setopt ($ curlobj, CURLOPT_POSTFIELDS, $ data );
  14. Curl_setopt ($ curlobj, CURLOPT_HTTPHEADER, array ("application/x-www-form-urlencoded; charset = utf-8 ",
  15. "Content-length:". strlen ($ data)
  16. ));
  17. Curl_exec ($ curlobj); // execute
  18. Curl_setopt ($ curlobj, CURLOPT_URL, "http://www.imooc.com/space/index ");
  19. Curl_setopt ($ curlobj, CURLOPT_POST, 0 );
  20. Curl_setopt ($ curlobj, CURLOPT_HTTPHEADER, array ("Content-type: text/xml"
  21. ));
  22. $ Output = curl_redir_exec ($ curlobj); // execute
  23. Curl_close ($ curlobj); // close cURL
  24. Echo $ output;
  25. /**
  26. * Custom page link jump capture
  27. */
  28. Function curl_redir_exec ($ ch, $ debug = "")
  29. {
  30. Static $ curl_loops = 0;
  31. Static $ curl_max_loops = 20;
  32. If ($ curl_loops ++ >=$ curl_max_loops)
  33. {
  34. $ Curl_loops = 0;
  35. Return FALSE;
  36. }
  37. Curl_setopt ($ ch, CURLOPT_HEADER, true); // enable the header to capture the redirected URL.
  38. Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true );
  39. $ Data = curl_exec ($ ch );
  40. // Split the returned content
  41. $ H_len = curl_getinfo ($ ch, CURLINFO_HEADER_SIZE );
  42. $ Header = substr ($ data, 0, $ h_len );
  43. $ Data = substr ($ data, $ h_len-1 );
  44. $ Http_code = curl_getinfo ($ ch, CURLINFO_HTTP_CODE );
  45. If ($ http_code = 301 | $ http_code = 302 ){
  46. $ Matches = array ();
  47. Preg_match ('/Location :(.*?) \ N/', $ header, $ matches );
  48. $ Url = @ parse_url (trim (array_pop ($ matches )));
  49. // Print_r ($ url );
  50. If (! $ Url)
  51. {
  52. // Couldn't process the url to redirect
  53. $ Curl_loops = 0;
  54. Return $ data;
  55. }
  56. $ Last_url = parse_url (curl_getinfo ($ ch, CURLINFO_EFFECTIVE_URL ));
  57. If (! Isset ($ url ['scheme '])
  58. $ Url ['scheme '] = $ last_url ['scheme'];
  59. If (! Isset ($ url ['host'])
  60. $ Url ['host'] = $ last_url ['host'];
  61. If (! Isset ($ url ['path'])
  62. $ Url ['path'] = $ last_url ['path'];
  63. $ New_url = $ url ['scheme ']. '://'. $ url ['host']. $ url ['path']. (isset ($ url ['query'])? '? '. $ Url ['query']: '');
  64. Curl_setopt ($ ch, CURLOPT_URL, $ new_url );
  65. Return curl_redir_exec ($ ch );
  66. } Else {
  67. $ Curl_loops = 0;
  68. Return $ data;
  69. }
  70. }
  71. ?>


Curl, PHP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.