【发布时间】:2018-03-14 08:47:10
【问题描述】:
我想通过以下网址在我的脚本中获取受密码保护的 xml 文件:
account@domain.com:password@domain2.com/file.xml
我发现在网页浏览器版本中工作(只需在浏览器中输入,xml 就会出现,没有重定向或密码提示):
account%40domain.com:password@domain2.com/file.xml
所以我尝试将此 url 与 simple_load_file() 和 file_get_contents() 一起使用,但它不起作用(由于 url 中有特殊字符,我知道此函数需要 urlencode 但account%40domain.com:password%40domain2.com/file.xml 不起作用)。
所以我尝试在 stackoverlow 上找到另一个解决方案:
$username = 'account@domain.com';
$password = 'password';
$context = stream_context_create(array(
'http' => array(
'header' => "Authorization: Basic " . base64_encode("$username:$password")
)
));
$data = file_get_contents('domain2.com/file.xml', false, $context);
我得到错误:
...failed to open stream: No such file or directory in...
我也尝试 curl,也没有成功(返回代码 302)。我不知道解决这个问题。有人可以帮帮我吗?
【问题讨论】:
-
302 是一个重定向代码,您应该从服务器响应中读取建议的 URL 并向该位置执行新请求以获取内容。