Hi Guys and girls,

I'm doing some work with XML documents and one is stored behind a .htpasswd arrangement. I have a script which requests the xml docs using XMLReader.

So the problem is the secure xml file, does anyone know of a way to access the file without going there and downloading via a browser. I'm hoping there'll be a nifty solution using headers and such.

Any help is much appreciated!

Thanks,

Fost

Recommended Answers

All 3 Replies

I think that cURL functions are able to do that. You can set up a HTTP request and add parameters like a username/password combination. Take a look at the cURL functions reference

You can use file_get_contents(). Just add the user and password to the url in the form:

http://user:pass@example.com/path/to/file.xml

Or you can specify the http header to send in the stream context parameter.

eg:

$context = stream_context_create(array( 
    'http' => array( 
      'method'  => 'GET', 
      'header'  => sprintf("Authorization: Basic %s\r\n", base64_encode($username.':'.$password)), 
      'timeout' => 5, 
    ), 
  )); 
  $xml = file_get_contents('http://example.com/path/to/file.xml', false, $context);

see: http://www.php.net/manual/en/function.stream-context-create.php

Also lookup Basic Authentication in a search engine to get an idea of the headers being sent/received in the http request/response. :)

Hey,

Sorry about the late getting back but thanks digital-ether! The stream context worked perfectly!

Had loads of fun playing with this and got the end result I needed.

Great stuff! Thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.