Question: 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>
> 
> 
> 

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);

```

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

EDIT:

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

Answer: 
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);
}

```
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
                    (
                    )

            )

```