PHP Get XML from a remote URL with HTTP authentication

I have a url that gives xml output. This requires a username and password, which I can get through the browser using the format:

http: // username: password@url.com

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

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


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

I tried using curl and installed the user agent in the browser, but this still does not reflect the data.

EDIT:

I also tried using pear http request 2, which also gives the forbidden 403

+3
source share
5 answers

You should try something like this:

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

For XML with a base authorization URL, use

$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);
}
+3
source

- http://username:password@url.com - , ; HTTP-, HTTP- . , * simplexml_load_file * HTTP-, , :

fopen("http://$username:$password@url.com");
0

http://pear.php.net/package/HTTP_Request2

$basicAuthRequest = new HTTP_Request2('http://user:password@url.com');

curl CURL_USERPWD

0

. xml-. .

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

            )
0

All Articles