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:pa
http://pear.php.net/package/HTTP_Request2 allows you to do a
$basicAuthRequest = new HTTP_Request2('http://user:password@url.com');
There's also an example of using curl to set the CURL_USERPWD
of the request here
but it loads only integers. not loaded strings in xml content. refer the below set of result.
[2] => SimpleXMLElement Object
(
[id] => 145894
[name] => SimpleXMLElement Object
(
)
[description] => SimpleXMLElement Object
(
)
[start_date] => SimpleXMLElement Object
(
)
[end_date] => SimpleXMLElement Object
(
)
[allow_deep_link] => 1
[program_id] => 6981
[program_name] => SimpleXMLElement Object
(
)
[category_name] => SimpleXMLElement Object
(
)
[code] => SimpleXMLElement Object
(
)
[tracking_url] => SimpleXMLElement Object
(
)
)
For XML with basic auth URL try this
$username = 'admin';
$password = 'mypass';
$server = 'myserver.com';
$context = stream_context_create(array(
'http' => array(
'header' => "Authorization: Basic " . base64_encode("$username:$password")
)
)
);
$data = file_get_contents("http://$server/", false, $context);
$xml=simplexml_load_string($data);
if ($xml === false) {
echo "Failed loading XML: ";
foreach(libxml_get_errors() as $error) {
echo "<br>", $error->message;
}
} else {
print_r($xml);
}
The format -- http://username:password@url.com -- is a convention the browser understands; but if you're making an HTTP request programmatically, you'll need to set the HTTP Headers for basic authentication. I don't think *simplexml_load_file* supports HTTP headers, but you could try using for example:
fopen("http://$username:$password@url.com");
You should try something like this :
$url = "http://username:password@url.com";
$xml = file_get_contents($url);
$data = new SimpleXMLElement($xml);