1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| <?php
* 自定义实现页面链接跳转抓取 * */ $data='username=demo_peter@126.com&password=123qwe&remember=1'; $curlobj = curl_init(); curl_setopt($curlobj, CURLOPT_URL, "http://www.imooc.com/user/login"); curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, true);
date_default_timezone_set('PRC'); curl_setopt($curlobj, CURLOPT_COOKIESESSION, TRUE); curl_setopt($curlobj, CURLOPT_HEADER, 0);
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); 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); curl_close($curlobj); echo $output;
* 自定义实现页面链接跳转抓取 */ 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); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $data = curl_exec($ch); $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))); if (!$url) { $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; } } ?>
|