curl模擬自動登錄&採集網頁數據

<!DOCTYPE>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>模擬登陸測試</title>
</head>

<body>
<?php
    //curl模擬自動登錄認證 
    $url = ""; 
    $User_Agent="Mozilla/5.0 (Windows NT 6.1; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0";    
    $post_data = "muser=***&passwd=***";    
    $refer="http://***/";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);        //設置URL
    curl_setopt($ch, CURLOPT_HEADER, true);        //設置顯示響應頭
    curl_setopt($ch,CURLOPT_USERAGENT,$User_Agent); //設置代理瀏覽器
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT,5);//超時處理
    curl_setopt($ch,CURLOPT_REFERER,$refer);    //設置來源網站
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);//設置重定向
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);//設置參數
    $content = curl_exec($ch);

    $headArr = explode("\r\n", $content);        //解析url
    foreach ($headArr as $loop) {
        //echo htmlspecialchars($loop)."<br />";
      if(strpos($loop, "http://***/default.aspx?id=") !== false){//找到Location
            $id = trim(substr($loop, 46));
      }
      if(strpos($loop, "Set-Cookie") !== false){//找到Cookie
            $cookie = trim(substr($loop, 12));
      }
    }
    $rurl="****?id=".$id;
    echo "edengUrl:<br>".$rurl;
    echo "<br />";
    echo "setcookie:<br>".$cookie."<br />";
    curl_close($ch);
    
    
    //結果處理
    $url = $rurl; 
    $User_Agent="Mozilla/5.0 (Windows NT 6.1; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0";    
    $refer="http://****/";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);        //設置URL
    curl_setopt($ch, CURLOPT_COOKIE, $cookie);        //設置cookies
    curl_setopt($ch,CURLOPT_USERAGENT,$User_Agent); //設置代理瀏覽器
    curl_setopt($ch, CURLOPT_TIMEOUT,5);//超時處理
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch,CURLOPT_REFERER,$refer);    //設置來源網站
    $content = curl_exec($ch);
    
    //解析html
    echo "<br />";
    echo "<br />解析:";
    echo "<br />";
    $content=strip_tags($content);//去除html標籤
    echo $content;
    echo "<br />";
    echo "<br />";
    $sk=strpos(trim($content), "當前用戶")+28;
    $name = trim(substr($content,$sk,9));
    $arr=explode("    ",$name);
    echo $sk."<br />".$arr[0]."<br />";
        
    curl_close($ch); 
    
?>
</body>
</html>
相關文章
相關標籤/搜索