0

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:password@url.com

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

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


$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.

$username = "user";
$password = "pass";
$process = curl_init("http://user:pass@url.com/feed?feed_id=1");
//curl_setopt($process, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
curl_setopt($process, CURLOPT_HEADER, 1);
curl_setopt($process, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($process, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($process);
echo $return;

I also tried using pear's http request 2, which also gives a 403 forbidden

Is the server i'm trying to access refusing all connections via PHP or is there a way around this? Thanks!

David Cahill
  • 419
  • 2
  • 5
  • 9

1 Answers1

0

Seems the page you are requesting is very picky. Make sure to send all the headers exactly as the browser does.

  • Install Firebug in Firefox, or use Chrome's inspector
  • Write down all the headers that get sent when you request the protected page
  • Send the headers from your php script exactly as the browser does.
  • If it does not work, use Wireshark to verify that you really got it right.

I suspect the page also looks for a user-agent header or so, not only the password.

Another problem could be that you have a different IP than your server, and that only your IP is whitelisted.

cweiske
  • 30,033
  • 14
  • 133
  • 194