使用PHP curl類比瀏覽器抓取網站資訊

來源:互聯網
上載者:User

標籤:header   set   開啟   deb   簡單   pbr   操作   style   地方   

curl是一個利用URL文法在命令列方式下工作的檔案傳輸工具。curl是一個利用URL文法在命令列方式下工作的檔案傳輸工具。
它支援很多協議:FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE 以及 LDAP。curl同樣支援HTTPS認證,HTTP POST方法, HTTP PUT方法, FTP上傳, kerberos認證, HTTP上傳, Proxy 伺服器, cookies, 使用者名稱/密碼認證, 下載檔案斷點續傳,
上傳檔案斷點續傳, httpProxy 伺服器管道( proxy tunneling), 甚至它還支援IPv6, socks5Proxy 伺服器, 通過httpProxy 伺服器上傳檔案到FTP伺服器等等,功能十分強大。

curl講解一

PHP中curl函數應用
簡單的來說一共四步
curl_init();
curl_setopt();
curl_exec();
curl_close();

最重要的命令就是 curl_setopt();

一個簡單的post請求例子
index.php

複製代碼 代碼如下:
<?php
$url = "http://www.mytest.com/curl/login.php"; //請求的url地址
$user = "zkg111"; //使用者名稱
$pass = "123456";
$postdata = "user_name=".$user."&password=".$pass; //請求的資料,以 & 符號分割
$curl = curl_init(); //開啟curl
curl_setopt($curl, CURLOPT_URL, $url); //佈建要求地址
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //是否輸出 1 or true 是不輸出 0 or false輸出
curl_setopt($curl, CURLOPT_POST, 1); //是否使用post方法請求
curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata); //post資料


echo $data = curl_exec($curl); //執行curl操作
curl_close($curl);
?>


下面一個簡單的例子,我隨便開啟了兄弟連的論壇,接著就類比了一下兄弟連論壇的登陸,如果需要發帖的話原理都是一樣的,轉接頁面,提交資料
特別注意的是cookie的儲存目錄 windows7下面必須是在./temp目錄下,開始我自己建立了一個新檔案夾,發現存是對的,但是cookie讀取的時候不對,為此還在好多地方
提問,但是沒有回答對的,折騰了好幾天該了儲存檔案為./temp目錄下才可以的,提醒別的朋友別和我一樣瞎轉

複製代碼 代碼如下:
<?php
$url = "http://bbs.lampbrother.net/login.php";
$urls = "http://bbs.lampbrother.net";
$lgt = 0;
$user = "XXXX";
$pass = "XXXX";
$question = 0;
$hideid = 1;
$cookie_file = tempnam(‘./temp‘,‘cookie‘);
$postdata = "forward=&jumpurl=".$urls."&step=2&lgt=".$lgt."&pwuser=".$user."&pwpwd=".$pass."&question=".$question."&answer=&hideid=".$hideid;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$data = curl_exec($ch);
curl_close($ch);
//echo $data;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, ‘http://bbs.lampbrother.net/‘);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
curl_exec($ch);
curl_close($ch);
?>

使用PHP curl類比瀏覽器抓取網站資訊

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.