- $data='username=zjzhoufy@126.com&password=1q2w3e&remember=1';
- $curlobj = curl_init(); // Initialization
- curl_setopt($curlobj, CURLOPT_URL, "http: //www.imooc.com/user/login"); // Set the URL to access the web page
- curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, true); // Do not print it out directly after execution
- // Cookie related settings, this part The setting needs to be set before all sessions start
- date_default_timezone_set('PRC'); // When using cookies, the time zone must be set first
- 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 will Let cURL 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;
- ?>
Copy code
The above code is not perfect. We should store the cookie in the cache and should not persist it, and such persistence can only be accessed by a single user.
So, actually we just need to set
Just do the following
- $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"); // Set the URL to access the web page
- curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, true); // Do not print it out directly after execution
- // Cookie related Settings, this part of the setting needs to be set before all sessions start
- date_default_timezone_set('PRC'); // When using cookies, the time zone must be set first
- 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 this setting must turn off safe mode and open_basedir, which is not good for 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;
- /**
- * Customized implementation of page link jump and 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, CURLO PT_HEADER, true ); // Turn on the header to capture the new URL redirected to
- 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 to
- $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;
- }
- }
- ?>
Copy code
|