PHP通过HTTP身份验证从远程URL获取XML [英] PHP Get XML from Remote URL with HTTP Authentication

查看:254
本文介绍了PHP通过HTTP身份验证从远程URL获取XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个提供xml输出的URL。它需要一个用户名和密码,我可以通过以下格式通过浏览器访问该用户名和密码:

I have a URL which gives an xml output. It requires a username and password which I can access through a browser using the format:


http:// username:password@url.com

但是,当我尝试通过php文件访问它时,我被禁止使用403:

However when I try to access it through a php file I get a 403 forbidden:

$url = "http://username:password@url.com";


$xml = @simplexml_load_file($url);
print_r($http_response_header);

我尝试使用curl并将用户代理设置为浏览器,但这仍然没有回显数据。

I have tried using curl and setting the user agent to a browser but this still doesn't echo the data.

编辑:

我还尝试了使用梨的http请求2,该请求也给出了403禁止

I also tried using pear's http request 2, which also gives a 403 forbidden

推荐答案

您应尝试以下操作:

$url = "http://username:password@url.com";
$xml = file_get_contents($url);
$data = new SimpleXMLElement($xml);

这篇关于PHP通过HTTP身份验证从远程URL获取XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆