Jump to content

[SOLVED] Trouble with CURL and HTTP Authentication...???


angelleye

Recommended Posts

I've got the following:

 

<?php
$url = "my_url";
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_USERPWD, "my_username:my_password");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
$xmlResponse = curl_exec($ch);
curl_close($ch);
?>

 

The response I'm getting back is simply: <h1>Bad Request</h1>

 

When I type my_url directly into a web browser a un/pw dialog pops up and when i use my_username and my_password it displays the XML on the screen as expected.  Also, I have an ASP script that is currently working just fine hitting this same URL with the same credentials.

 

Any information on what I'm doing wrong with my PHP/CURL would be greatly appreciated.  Thanks!

Try not using CURLOPT_POST and also try CURLOPT_URL, "$url".  I'm looking at your example and the one I have working and there's not much of a difference.  Here's mine:

 

   $curl = curl_init();
   curl_setopt ($curl, CURLOPT_URL, "$url");
   curl_setopt($curl, CURLOPT_USERPWD, "user:pass");
   curl_setopt($curl, CURLOPT_USERAGENT, $useragent); //Spoofs Firefox
   curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
   $result = curl_exec ($curl);
   curl_close ($curl);

It didn't like me adding the querystring paramaters onto the $url string.  I removed them from that and added them to a $postFields string which I then included with CURLOPT_POSTFIELDS.

 

$url = "http://www.domain.com";
$postFields = "Param1=Val1&Param2=Val2";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, "username:password");
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
$xmlResponse = curl_exec($ch);

 

And that is working now.  I guess it might have helped if I'd have included those details in my original post!  Doh!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.