David Cahill
David Cahill

Reputation: 519

PHP Get XML from Remote URL with HTTP Authentication

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:[email protected]

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

$url = "http://username:[email protected]";


$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

Upvotes: 5

Views: 15218

Answers (5)

Rajendra
Rajendra

Reputation: 1434

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

Upvotes: 5

Weso Development Team
Weso Development Team

Reputation: 23

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

            )

Upvotes: 0

HamZa
HamZa

Reputation: 14921

You should try something like this :

$url = "http://username:[email protected]";
$xml = file_get_contents($url);
$data = new SimpleXMLElement($xml);

Upvotes: 7

AndrewPK
AndrewPK

Reputation: 6150

http://pear.php.net/package/HTTP_Request2 allows you to do a

$basicAuthRequest = new HTTP_Request2('http://user:[email protected]');

There's also an example of using curl to set the CURL_USERPWD of the request here

Upvotes: 0

McGarnagle
McGarnagle

Reputation: 102763

The format -- http://username:[email protected] -- 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:[email protected]");

Upvotes: 1

Related Questions