Page 1 of 1

cURL and http identification

Posted: Sat May 17, 2008 7:33 pm
by nickzer
Hi guys,

I never did this, so I don't know where I should start. I'm doing an API for a PHP application, that needs user and password identification. I see in other APIs that, using cURL, are able to "post" user credentials... but I'm unable to get that. How can I do it?

Thank you in advance!

Re: cURL and http identification

Posted: Sun May 18, 2008 10:26 am
by sim-and-sim
ha, my third post here and they're both on the same place...

its simple really to post using cURL, see code below...

Code: Select all

 
 
$ch = curl_init();  //This here just starts the cURL, kinda like session_start();
 
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "$URL"); //Set the file you want to post to here
 
 
// Do a POST
$data = array("var1" => "value1", "var2" => "value2"); //These are your post fields, the first is the post name just like the name="var1" in a HTML form, 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //This returns it as a var, it'll be explained at the end
curl_setopt($ch, CURLOPT_POST, true); //Tells the cURL you want to post values
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //Puts the POST Values into the post
 
// grab URL, and print
$output = curl_exec($ch); //This is your file returned, this is what the 3rd option up from this does, without that option this will just be printed out at this point... 
 
curl_close($ch); //This closes the cURL... go figure....
 
I hope that helps you...

Re: cURL and http identification

Posted: Sun May 18, 2008 10:41 am
by nickzer
sim-and-sim wrote:ha, my third post here and they're both on the same place...

its simple really to post using cURL, see code below...

Code: Select all

 
 
$ch = curl_init();  //This here just starts the cURL, kinda like session_start();
 
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "$URL"); //Set the file you want to post to here
 
 
// Do a POST
$data = array("var1" => "value1", "var2" => "value2"); //These are your post fields, the first is the post name just like the name="var1" in a HTML form, 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //This returns it as a var, it'll be explained at the end
curl_setopt($ch, CURLOPT_POST, true); //Tells the cURL you want to post values
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //Puts the POST Values into the post
 
// grab URL, and print
$output = curl_exec($ch); //This is your file returned, this is what the 3rd option up from this does, without that option this will just be printed out at this point... 
 
curl_close($ch); //This closes the cURL... go figure....
 
I hope that helps you...
Hey sim-and-sim,

You're wrong! I'm not asking how to use cURL. I'm asking how to GET with PHP the HTTP login credentials sent by cURL.

Re: cURL and http identification

Posted: Sun May 18, 2008 11:31 am
by onion2k
All Curl does is mimic a web browser. The way your script works should be exactly the same as if the user was using Firefox or Internet Exploer ... If the login details are sent in the URL you'd access them in the script using $_GET, if they're sent as part of the POST variables you'd access them with $_POST.

Re: cURL and http identification

Posted: Sun May 18, 2008 11:41 am
by nickzer
onion2k wrote:All Curl does is mimic a web browser. The way your script works should be exactly the same as if the user was using Firefox or Internet Exploer ... If the login details are sent in the URL you'd access them in the script using $_GET, if they're sent as part of the POST variables you'd access them with $_POST.
Yes, I know, but isn't exactly the thing I'm wanting. I'm talking about the HTTP BASIC AUTHENTICATION, that can be used by cURL.

Re: cURL and http identification

Posted: Sun May 18, 2008 12:57 pm
by onion2k
That's for passing a username and password to an htaccess type of password protection. It's not really anything to do with PHP.

Re: cURL and http identification

Posted: Sun May 18, 2008 1:47 pm
by nowaydown1
nickzer: If you want to have curl do your basic auth stuff.... just make your request url like http://user:pass@whatever.com. Should work for you.

Re: cURL and http identification

Posted: Sun May 18, 2008 1:51 pm
by nowaydown1
I'm talking out my butt today, sorry try this:

Code: Select all

 
$u = "test";
$p = "test";
            
curl_setopt($hCURL, CURLOPT_URL, "somehost.com/whatever.php");       
curl_setopt($hCURL, CURLOPT_RETURNTRANSFER,1);
curl_setopt($hCURL, CURLOPT_TIMEOUT, 6);
curl_setopt($hCURL, CURLOPT_POST, 1 );
curl_setopt($hCURL, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($hCURL, CURLOPT_USERPWD, $u . ':' . $p);
 

Re: cURL and http identification

Posted: Sun May 18, 2008 2:13 pm
by nickzer
nowaydown1 wrote:I'm talking out my butt today, sorry try this:

Code: Select all

 
$u = "test";
$p = "test";
            
curl_setopt($hCURL, CURLOPT_URL, "somehost.com/whatever.php");       
curl_setopt($hCURL, CURLOPT_RETURNTRANSFER,1);
curl_setopt($hCURL, CURLOPT_TIMEOUT, 6);
curl_setopt($hCURL, CURLOPT_POST, 1 );
curl_setopt($hCURL, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($hCURL, CURLOPT_USERPWD, $u . ':' . $p);
 
Thanks! :)