tags:

views:

281

answers:

5

So for example here is a URL: https://rexms.net:32005/rexwapi/common/timeframes -- if I go to that URL in my browser enter the correct username/password it will spit out XML at me. The problem is I need to access this through PHP so I obviously don't get a prompt to enter username/password.

Current code is:

$timeframes_xml = simplexml_load_file(file_get_contents('https://rexms.net:32005/rexwapi/common/timeframes'));
+6  A: 

I think you can simply use

https://username:[email protected]:32005/rexwapi/common/timeframes
David Caunt
This seems inconsistent. I did it in PHP and it didn't return results, same when I tried in MSIE buuuut when I tried in Google Chrome it DID work, any ideas on what may be causing that?
Andrew G. Johnson
IE disabled this as it's implementation had security flaws. PHP parses the data and sets it as required :)
David Caunt
So this shouldn't work with PHP?
Andrew G. Johnson
The opposite - it should work
David Caunt
+4  A: 

You need to use curl with curl_setopt to set the CURLOPT_USERPWD option.

Dennis Palmer
I've tried doing this a few ways and can't seem to get results, is there anyone you could write a code sample?
Andrew G. Johnson
+1  A: 

It may be best to approach this using three PEAR packages: HTTP/Socket/Net. With the HTTP package you are able to make REST requests to a Basic Auth realm easily.

For more check out: How To: Making a PHP REST client to call REST resources

Jim Walker
+2  A: 

You need to set the Http header Authorization. Set the value to a base64 encoded version of

Basic username:password

That is assuming the server is using basic auth. On Windows you could use Fiddler to see what type of auth the server is requesting.

Darrel Miller
A: 

Found my answer in another SO question

function get_rex_xml($url)
{
    $curl = curl_init($url);
    curl_setopt_array($curl, array(CURLOPT_RETURNTRANSFER => TRUE,CURLOPT_USERPWD => 'username:password'));
    ob_start();
    $response_xml = curl_exec($curl);
    ob_end_clean();
    curl_close($curl);
    return $response_xml;
}

header('content-type:text/xml');
echo get_rex_xml('https://rexms.net:32005/rexwapi/common/timeframes');
Andrew G. Johnson