0

I am trying to fire a HTTP GET request on a secured URL which asks for username and password. This is fine when I am using that from browser but I am not sure how to do that using PHP.

I have tried using the two methods:

1) Using Curl as suggested in here: Make a HTTPS request through PHP and get response

2) Using the file_get_contents as suggested in here: How to send a GET request from PHP?

But the first one didn't give me any response back. And the second one gave me the following error:

failed to open stream: HTTP request failed

And this is my code for the curl:

$url="https://xxxxx.com";
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;

and for the file_get_contents:

$url="https://xxxx.com";
$response=file_get_contents($url);
echo $response;

The URL will return a XML response for a API I am testing. Can someone point me to the right direction?

Thanks!

Community
  • 1
  • 1
blackRider
  • 143
  • 2
  • 17
  • 1
    You might be confusing access restrictions (eg basic authentication & co) and https - are you sure the server requires https? And if you need basic authentication, you have to tell curl to give the webserver a username and password. – fvu Oct 08 '12 at 23:41

1 Answers1

0

If we focus on the requirement to send a username and password because I suspect that's your main problem, try this

$ch = curl_init();

$url="https://xxxxx.com";
// OR - check with your server's operator
$url="http://xxxxx.com";

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
// or maybe 
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
// - see http://stackoverflow.com/questions/4753648/problems-with-username-or-pass-with-colon-when-setting-curlopt-userpwd
// check the cURL documentation

$output = curl_exec($ch);
$info = curl_getinfo($ch);
// don't forget to check the content of $info, even a print_r($info) is better 
// than nothing during debug
curl_close($ch);
fvu
  • 32,488
  • 6
  • 61
  • 79
  • Thank you for the quick response fvu. I have tried your code but still no luck with that. And $info is just printing out the text "Array". – blackRider Oct 09 '12 at 00:16
  • And yes the server requires https. otherwise I am getting a 404 error. – blackRider Oct 09 '12 at 00:16
  • @meraz are you sure you used `print_r` ? Because it should either show FALSE (on error) or a full set of variables that help you diagnose the transfer or problems. – fvu Oct 09 '12 at 00:21
  • oops sorry my bad. I have used echo instead of print_r. Ok so print_r looks like this: ( [http_code] => 0 [header_size] => 0 [request_size] => 0 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.05831 [pretransfer_time] => 0 [size_upload] => 0 [size_download] => 0 [speed_download] => 0 [speed_upload] => 0 [download_content_length] => -1 [upload_content_length] => -1 [starttransfer_time] => 0 [redirect_time] => 0 [certinfo] => Array ( ) [redirect_url] => ) – blackRider Oct 09 '12 at 01:03