cURL and http identification

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nickzer
Forum Newbie
Posts: 4
Joined: Sat May 17, 2008 7:32 pm

cURL and http identification

Post 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!
sim-and-sim
Forum Newbie
Posts: 6
Joined: Sun May 18, 2008 9:54 am

Re: cURL and http identification

Post 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...
nickzer
Forum Newbie
Posts: 4
Joined: Sat May 17, 2008 7:32 pm

Re: cURL and http identification

Post 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.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: cURL and http identification

Post 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.
nickzer
Forum Newbie
Posts: 4
Joined: Sat May 17, 2008 7:32 pm

Re: cURL and http identification

Post 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.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: cURL and http identification

Post 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.
nowaydown1
Forum Contributor
Posts: 169
Joined: Sun Apr 27, 2008 1:22 am

Re: cURL and http identification

Post 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.
nowaydown1
Forum Contributor
Posts: 169
Joined: Sun Apr 27, 2008 1:22 am

Re: cURL and http identification

Post 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);
 
nickzer
Forum Newbie
Posts: 4
Joined: Sat May 17, 2008 7:32 pm

Re: cURL and http identification

Post 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! :)
Post Reply