簡體   English   中英

php curl-使用HTTP身份驗證訪問URL(需要幫助)

[英]php curl - accessing url with http authentication (need help)

我在HTTP身份驗證中有問題。 我無法獲取此url的內容,因為它需要http auth。

碼:

<?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:password@www.abcdefg.com/12345/”,該方法無效。

訪問此url(1)時,會彈出一個窗口詢問用戶名和密碼。 但彈出窗口來自另一個url(2)(一個sso身份驗證服務器)。 如果通過身份驗證。 然后它又回到url(1)。 目前,我可以訪問此url(1)的內容。

我使用firebug通過直接從瀏覽器訪問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

第三步

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

第四步

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

然后顯示內容…

這與Cookie有關嗎? 如何使用php curl讀取內容?

您必須設置:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

這將使cURL服從302並生成其他請求。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM