-
- $ Data = 'username = zjzhoufy@126.com & password = 1q2w3e & remember = 1 ';
- $ Curlobj = curl_init (); // Initialization
- Curl_setopt ($ curlobj, CURLOPT_URL, "http://www.imooc.com/user/login"); // sets the URL for accessing the web page
- Curl_setopt ($ curlobj, CURLOPT_RETURNTRANSFER, true); // It is not printed directly after execution.
- // Cookie settings. these settings must be set before all sessions start.
- Date_default_timezone_set ('prc'); // you must set the time zone before using cookies.
- Curl_setopt ($ curlobj, CURLOPT_COOKIESESSION, TRUE );
- Curl_setopt ($ curlobj, CURLOPT_COOKIEJAR, 'cookie.txt '); // Save
- Curl_setopt ($ curlobj, CURLOPT_COOKIEFILE, 'cookie.txt '); // Read
- Curl_setopt ($ curlobj, CURLOPT_HEADER, 0 );
- Curl_setopt ($ curlobj, CURLOPT_FOLLOWLOCATION, 1); // This allows cURL to support page link jump
- Curl_setopt ($ curlobj, CURLOPT_COOKIE, session_name (). '='. session_id ());
- Curl_setopt ($ curlobj, CURLOPT_POST, 1 );
- Curl_setopt ($ curlobj, CURLOPT_POSTFIELDS, $ data );
- Curl_setopt ($ curlobj, CURLOPT_HTTPHEADER, array ("application/x-www-form-urlencoded; charset = utf-8 ",
- "Content-length:". strlen ($ data)
- ));
- Curl_exec ($ curlobj); // execute
- Curl_setopt ($ curlobj, CURLOPT_URL, "http://www.imooc.com/space/index ");
- Curl_setopt ($ curlobj, CURLOPT_POST, 0 );
- Curl_setopt ($ curlobj, CURLOPT_HTTPHEADER, array ("Content-type: text/xml"
- ));
- $ Output = curl_exec ($ curlobj); // execute
- Curl_close ($ curlobj); // close cURL
- Echo $ output;
- ?>
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
- CURLOPT_COOKIESESSION
As follows:
-
- $ Data = 'username = demo_demo@126.com & password = 123456qwe & remember = 1 ';
- $ Curlobj = curl_init (); // Initialization
- Curl_setopt ($ curlobj, CURLOPT_URL, "http://www.imooc.com/user/login"); // sets the URL for accessing the web page
- Curl_setopt ($ curlobj, CURLOPT_RETURNTRANSFER, true); // It is not printed directly after execution.
- // Cookie settings. these settings must be set before all sessions start.
- Date_default_timezone_set ('prc'); // you must set the time zone before using cookies.
- Curl_setopt ($ curlobj, CURLOPT_COOKIESESSION, TRUE );
- Curl_setopt ($ curlobj, CURLOPT_HEADER, 0 );
- Curl_setopt ($ curlobj, CURLOPT_COOKIE, session_name (). '='. session_id ());
- // Comment out this line because the security mode and open_basedir must be disabled for this setting, which is not conducive to server security
- // Curl_setopt ($ curlobj, CURLOPT_FOLLOWLOCATION, 1 );
- Curl_setopt ($ curlobj, CURLOPT_POST, 1 );
- Curl_setopt ($ curlobj, CURLOPT_POSTFIELDS, $ data );
- Curl_setopt ($ curlobj, CURLOPT_HTTPHEADER, array ("application/x-www-form-urlencoded; charset = utf-8 ",
- "Content-length:". strlen ($ data)
- ));
- Curl_exec ($ curlobj); // execute
- Curl_setopt ($ curlobj, CURLOPT_URL, "http://www.imooc.com/space/index ");
- Curl_setopt ($ curlobj, CURLOPT_POST, 0 );
- Curl_setopt ($ curlobj, CURLOPT_HTTPHEADER, array ("Content-type: text/xml"
- ));
- $ Output = curl_redir_exec ($ curlobj); // execute
- Curl_close ($ curlobj); // close cURL
- Echo $ output;
- /**
- * Custom page link jump capture
- */
- Function curl_redir_exec ($ ch, $ debug = "")
- {
- Static $ curl_loops = 0;
- Static $ curl_max_loops = 20;
- If ($ curl_loops ++ >=$ curl_max_loops)
- {
- $ Curl_loops = 0;
- Return FALSE;
- }
- Curl_setopt ($ ch, CURLOPT_HEADER, true); // enable the header to capture the redirected URL.
- Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true );
- $ Data = curl_exec ($ ch );
- // Split the returned content
- $ H_len = curl_getinfo ($ ch, CURLINFO_HEADER_SIZE );
- $ Header = substr ($ data, 0, $ h_len );
- $ Data = substr ($ data, $ h_len-1 );
- $ Http_code = curl_getinfo ($ ch, CURLINFO_HTTP_CODE );
- If ($ http_code = 301 | $ http_code = 302 ){
- $ Matches = array ();
- Preg_match ('/Location :(.*?) \ N/', $ header, $ matches );
- $ Url = @ parse_url (trim (array_pop ($ matches )));
- // Print_r ($ url );
- If (! $ Url)
- {
- // Couldn't process the url to redirect
- $ Curl_loops = 0;
- Return $ data;
- }
- $ Last_url = parse_url (curl_getinfo ($ ch, CURLINFO_EFFECTIVE_URL ));
- If (! Isset ($ url ['scheme '])
- $ Url ['scheme '] = $ last_url ['scheme'];
- If (! Isset ($ url ['host'])
- $ Url ['host'] = $ last_url ['host'];
- If (! Isset ($ url ['path'])
- $ Url ['path'] = $ last_url ['path'];
- $ New_url = $ url ['scheme ']. '://'. $ url ['host']. $ url ['path']. (isset ($ url ['query'])? '? '. $ Url ['query']: '');
- Curl_setopt ($ ch, CURLOPT_URL, $ new_url );
- Return curl_redir_exec ($ ch );
- } Else {
- $ Curl_loops = 0;
- Return $ data;
- }
- }
- ?>
|