简体   繁体   English

php curl-使用HTTP身份验证访问URL(需要帮助)

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

I have a problem in HTTP Authentication. 我在HTTP身份验证中有问题。 I couldn't get the content of this url because it need http auth. 我无法获取此url的内容,因为它需要http auth。

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,文档已移至此处”。

I've tried "http://username:password@www.abcdefg.com/12345/", doesn't work. 我试过了“ http:// username:password@www.abcdefg.com/12345/”,该方法无效。

When accessing this url(1), a popup window ask for username and password. 访问此url(1)时,会弹出一个窗口询问用户名和密码。 but the popup window is from another url(2)(a sso authentication server). 但弹出窗口来自另一个url(2)(一个sso身份验证服务器)。 if pass the authentication. 如果通过身份验证。 then it gets back to url(1) again. 然后它又回到url(1)。 at this time, I can access the content of this url(1). 目前,我可以访问此url(1)的内容。

I use firebug get 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 第三步

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? 这与Cookie有关吗? How can I use php curl to read the content? 如何使用php curl读取内容?

You have to set: 您必须设置:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

This will make cURL obey the 302 and generate an additional request. 这将使cURL服从302并生成其他请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM