現在來使用PHP 的cURL 開啟NTLM Authentication 連線,更威的是cURL 也可以通過 Basic Authentication, Digest Authentication, GSSNEGOTIATE Authentication...(其實這幾個只有NTLM 和Basic 我看得懂:P)
系統設定
- 先開啟php.ini (如果不確定php.ini 放在哪,可以參考phpinfo()的輸出結果)
- 搜尋「curl」,應該會找到;extension=php_curl.dll將前面分號(;)去掉
- 重新啟動Apache
- 系統設定完成,我們現在可以用cURL了
範例程式
接下來我們要寫一隻程式,這個程式可以登入被保護的網頁,下載網頁內容回來
// 初始化cURL 物件
$ch = curl_init();
//送出要求網頁的URL
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/_layouts/page1.aspx");
//設定HTTP 標頭版本(HTTP/1.1)
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
//送出驗證要求,這裡的CURLAUTH_ANY 代表可以通過上述任何驗證(包括NTLM Authentication)
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
//送出使用者帳密(帳號與密碼用冒號隔開)
curl_setopt($ch, CURLOPT_USERPWD, "function1122:password");
//設定不顯示伺服器回應標頭(200 OK 之類的)
curl_setopt($ch, CURLOPT_HEADER, false);
//將回應直接顯示出來
curl_exec($ch);
//關閉cURL 連線
curl_close($ch);
我們發現只要短短幾行就可以用PHP 通過NTLM 驗證了,我今天也是傻傻的找了半天fsockopen() 要如何去解析NTLM ,其實都沒有必要啊Orz
BTW, cURL 也可以讓php 發送POST 請求,這就自己研究啦:-)
參考資料
- http://www.php.net/manual/en/function.curl-setopt.php
- http://rabaix.net/articles/2008/3/13/using-soap-php-with-ntlm-authentication
上集:PHP 下載遠端網頁 (上)
喔,對了,這是我今天的成品:TCP Proxy - powered by FUNction (使用cURL)
讀者回應 ( 10 意見 )
訂閱張貼留言 (Atom)
What i do not understood is actually how you're no longer really much more well-appreciated than you might be right now. You are very intelligent. You recognize thus considerably with regards to this subject, made me personally consider it from numerous varied angles. Its like men and women aren't interested except it is one
thing to do with Girl gaga! Your personal stuffs excellent.
Always deal with it up!
Here is my blog post :: free iphone