php curl - 使用 http 身份验证访问 url(需要帮助)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10792419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php curl - accessing url with http authentication (need help)
提问by Strong
I have a problem in HTTP Authentication. I couldn't get the content of this url because it need http auth.
我在 HTTP 身份验证中遇到问题。我无法获取此 url 的内容,因为它需要 http 身份验证。
Code:
代码:
<?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);
?>
The Problem is: Instead of show the real content, it shows "302 Found, The document has moved here."
问题是:它没有显示真实内容,而是显示“302 Found,文档已移至此处”。
I've tried "http://username:[email protected]/12345/", doesn't work.
我试过“http://username:[email protected]/12345/”,没有用。
When accessing this url(1), a popup window ask for username and password. but the popup window is from another url(2)(a sso authentication server). if pass the authentication. then it gets back to url(1) again. at this time, I can access the content of this url(1).
访问此 url(1) 时,会弹出一个窗口,要求输入用户名和密码。但弹出窗口来自另一个 url(2)(一个 sso 身份验证服务器)。如果通过认证。然后它再次返回到 url(1)。这时候就可以访问这个url(1)的内容了。
I use firebugget the following message by accessing the url directly from browser:
我使用firebug通过直接从浏览器访问 url 来获取以下消息:
Step 1
第1步
URL: GET http://www.abcdefg.com/12345/
Status: 302 Found
Protocol: http
Step 2
第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
Step 3
第 3 步
URL: GET http://www.abcdefg.com/obrar.cgi?cookie=xxxxxxxxxxxxxxx
Status: 302 Found
Protocol: http
Step 4
第四步
URL: GET http://www.abcdefg.com/12345/
Status: 200 OK
Protocol: http
Then display the content…
然后显示内容...
Is this something to do with the cookie? How can I use php curl to read the content?
这和cookie有关吗?如何使用 php curl 读取内容?
采纳答案by ziad-saab
You have to set:
你必须设置:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
This will make cURL obey the 302 and generate an additional request.
这将使 cURL 遵守 302 并生成额外的请求。

