PHP 網頁擷取

  • 6033
  • 0

摘要:PHP 網頁擷取

01 /// $targetURL: string
02 /// $urlRefereral: string
03 /// $auth = "username=$s_newsid" . "&password=$s_newspwd" . "&submit=確定";
04 function getPage($targetURL, $urlRefereral, $auth)
05 {
06     $URL="http://www.mlc.edu.tw/index.php?module=school&controller=inner-board&action=login"; // 填入論壇的登錄頁面地址
07     $ch = curl_init();
08     curl_setopt($ch, CURLOPT_URL, $URL);
09     curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
10     curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
11     curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)');
12     curl_setopt($ch, CURLOPT_REFERER, $urlRefereral); //設置,訪問頁面的來源地址
13     curl_setopt($ch, CURLOPT_POST, 1);
14     curl_setopt($ch, CURLOPT_POSTFIELDS, $auth); // 登錄頁面,把用戶名,密碼分別對應起來
15     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
16     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 設定curl_exec回傳值
17     curl_exec($ch);
18
19       $html = curl_exec($ch);
20
21       if (curl_errno($ch))
22     {
23         print curl_error($ch);
24        return false;
25     }

26     else
27         curl_close($ch);
28
29       return $html;
30 }