2012-05-29 78 views
5

我在HTTP身份驗證中遇到問題。我無法獲取此網址的內容,因爲它需要http auth。php curl - 使用http身份驗證訪問url(需要幫助)

代碼:

<?php 

$url = "http://www.abcdefg.com/12345/"; 

$username = 'username'; 
$password = 'password'; 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); 
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password"); 

$out = curl_exec($ch); 

print "error:" . curl_error($ch) . "<br />"; 
print "output:" . $out . "<br /><br />"; 

curl_close($ch); 

?> 

的問題是: 而是展示真實的內容,它顯示「302發現,該文件已經搬到這裏。」

我試過「http:// username:[email protected]/12345/」,不起作用。

訪問此URL(1)時,彈出窗口詢問用戶名和密碼。但彈出窗口來自另一個URL(2)(一個sso認證服務器)。如果通過認證。然後它又回到url(1)。在這個時候,我可以訪問這個url的內容(1)。

我使用螢火通過直接從瀏覽器訪問URL得到以下信息:

步驟1

URL: GET http://www.abcdefg.com/12345/ 
Status: 302 Found 
Protocol: http 

步驟2

URL: GET https://www.ssoauth.com/obrareq.cgi?wh=xxx wu=xxx wo=xxx rh=http://www.abcdefg.com ru=/12345/ 
Status: 302 Redirect 
Protocol: https 

步驟3

URL: GET http://www.abcdefg.com/obrar.cgi?cookie=xxxxxxxxxxxxxxx 
Status: 302 Found 
Protocol: http 

步驟4

URL: GET http://www.abcdefg.com/12345/ 
Status: 200 OK 
Protocol: http 

然後顯示的內容...

這是什麼做的餅乾嗎? 如何使用php curl來讀取內容?

回答

5

你必須設置:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 

這將使捲曲服從302,併產生額外的請求。

+0

我只是說 curl_setopt($ CH,CURLOPT_FOLLOWLOCATION,真) 但它顯示 「在第32行解析錯誤」 線32: curl_setopt($ CH,CURLOPT_HTTPAUTH,CURLAUTH_ANY); 我是否需要添加與https相關的信息?因爲**步驟2 **,它使用HTTPS端口443. – Strong

+0

我忘了';'在行尾! –

+0

opps,錯過了「;」... – Strong