开发者

load xml file with authentication (and improving security)

开发者 https://www.devze.com 2023-02-12 17:50 出处:网络
I\'m trying to use simplexml to load an 开发者_如何学运维xml file from a server: $xml = simplexml_load_file($xml_query);

I'm trying to use simplexml to load an 开发者_如何学运维xml file from a server:

$xml = simplexml_load_file($xml_query);

The server needs HTTP authentication, so (unsurprisingly) I get an error message:

HTTP request failed! HTTP/1.1 401 Unauthorized

Is it possible to pass my username and password to the server using this function?

EDIT

If anyone can tell me a more secure way to do this, I'd be grateful.


You should be able to prepend the username and password to the URL you're opening. Instead of

http://link.to/file.xml

try

http://username:password@link.to/file.xml


This is my first attempt at making it more secure, using cURL:

$ch = curl_init();

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
curl_setopt($ch, CURLOPT_URL, $xml_query);
curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);
$result = curl_exec($ch);
curl_close($ch);

$xml = simplexml_load_string($result);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号