【问题标题】:PHP simple_load_file and file_get_content with email and password带有电子邮件和密码的 PHP simplexml_load_file 和 file_get_contents
【发布时间】: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 并向该位置执行新请求以获取内容。

标签: php xml email passwords


【解决方案1】:

file_get_contents 将自动跟随重定向(例如 302),但您需要通过提供方案告诉它您正在获取远程文件。改变

$data = file_get_contents('domain2.com/file.xml', false, $context);

$data = file_get_contents('http://domain2.com/file.xml', false, $context);

(或 https://,如果适用)

如果你不提供方案,它会尝试在本地机器上打开一个文件——即domain2.com目录下名为file.xml的文件

【讨论】:

  • 感谢您的回答!你是对的,但现在我有另一个错误:...failed to open stream: Redirection limit reached, aborting in...
  • 这听起来更像是远程文件的问题,而不是代码的问题。但是,如果不知道实际的 URL,就不可能说出来。
  • 我可以通过私人按摩将实际网址发送给您吗?
猜你喜欢
  • 1970-01-01
  • 2012-08-24
  • 2020-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多