PHP:file_get_contents无法打开流:HTTP请求失败!HTTP/1.0 401未经授权


PHP: file_get_contents failed to open stream: HTTP request failed! HTTP/1.0 401 Unauthorized

当我尝试使用file_get_contents时,我收到以下错误消息:(URL已更改)。

Warning: file_get_contents(http://user:password@1.1.1.1/test/test.xml?cmd=show&user=*) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 401 Unauthorized in /srv/www/htdocs/phonebook.php on line 50

如果我调用URL,我可以在浏览器中看到XML文件。它之间没有空格。但是,我不明白。为什么它不起作用。PHP版本为5.3.17。我应该使用cUrl吗?

语法为:

$xml_echo = "http://".$user.":".$pass."@".$ip .$url;
echo $xml_echo;
$xml_string = file_get_contents($xml_echo);

我遇到的另一个问题,可能与描述的问题有关,是我在尝试使用时出错

$xml = new SimpleXMLElement(utf8_encode($xml_string));

我还尝试使用:simplexml_load_string

Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /srv/www/htdocs/phonebook.php:55 Stack trace: #0 /srv/www/htdocs/phonebook.php(55): SimpleXMLElement->__construct('') #1 {main} thrown in /srv/www/htdocs/get_pbx_phonebook.php on line 55

哦,发布后。我试着用

https://

而不是

http://

现在它奏效了。

所以,不需要任何帮助。