5

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

5 Answers 5

7

You should try something like this :

$url = "http://username:[email protected]";
$xml = file_get_contents($url);
$data = new SimpleXMLElement($xml);
Sign up to request clarification or add additional context in comments.

Comments

5

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

Comments

1

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

Comments

0

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

2 Comments

This still gives a 403 forbidden
did you try using curl and setting CURL_USERPWD?
0

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

            )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.