cURL and http identification
Moderator: General Moderators
cURL and http identification
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!
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
ha, my third post here and they're both on the same place...
its simple really to post using cURL, see code below...
I hope that helps you...
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....
Re: cURL and http identification
Hey sim-and-sim,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...
I hope that helps you...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....
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
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
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.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.
Re: cURL and http identification
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
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
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
Thanks!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);