cURL problems

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
jjfletch
Forum Newbie
Posts: 13
Joined: Mon Apr 18, 2005 5:42 am

cURL problems

Post by jjfletch »

Basically, there is a secured site, which I can log into and then add data via a form once I'm logged in. I can pass the login information in the URL, and I can get it to work if, for example, I type the address into my browser bar, like so:

Code: Select all

https://my.example.com/authentication.php?user=Username&pass=PW&destination=https://someurl.example.com/Something
This will bring me to the page I want to post form data to. Which is great, except that if I use this url in my code, it doesn't work. It just brings me back to the initial login page. I should also say that if I use:

Code: Select all

header("Location: https://my.example.com/authentication.p ... /Something");
I will be brought to the form that I want to post data to. The problem is that I don't want to actually redirect to the page, I just want to get to the form that I need to post information to.

If I attempt to call this URL in my code, it doesn't work. There are multiple redirects that occur if I type it in the address bar, and I've used FOLLOWLOCATION, 1, but that doesn't seem to help.

The latest iteration of code attempts to request the page that sets the cookies, and then make subsequent requests that include the cookies, but it still winds up kicking me back to the first page.

Code: Select all

$cookie_jar = tempnam('/tmp','cookie');

$c = curl_init('https://my.example.com/authentication.php?user=Username&pass=PW');
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($c, CURLOPT_COOKIEJAR, $cookie_jar);
$page = curl_exec($c);
curl_close($c);


$c = curl_init('https://someurl.example.com/Something');
curl_setopt($c, CURLOPT_POST, 1);
curl_setopt($c, CURLOPT_POSTFIELDS, 'field=value');
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_COOKIEFILE, $cookie_jar);
$page = curl_exec($c);
curl_close($c);
Can anyone help?
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

If that is your code it is normal, because you close the curl session after you initialise the login session. Then try to access protected page with newly opened session.

Maybe this could help:

Code: Select all

$cookie_jar = tempnam('/tmp','cookie');

$c = curl_init('https://my.example.com/authentication.php?user=Username&pass=PW');
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($c, CURLOPT_COOKIEJAR, $cookie_jar);
curl_setopt($ch, CURLOPT_URL, "https://someurl.example.com/Something");
$page = curl_exec($c);
curl_close($c);
jjfletch
Forum Newbie
Posts: 13
Joined: Mon Apr 18, 2005 5:42 am

Post by jjfletch »

Hi, thanks for your reply.

No, I think the problem has to do with the SSL certificate on the server I was trying to access.

I was able to figure out the first part... I am now able to retrieve the form I want to post to, but now, I can't figure out how to actually *post* to that form....


Here's the code thus far:

Code: Select all

$cookie_jar = tempnam('tmp/','cookie');

$url = "https://my.example.com/authentication.php";
$POSTFIELDS = 'username=UNAME&password=PW&destination=https://my.example.com/someotherpage
$reffer = "https://my.example.com/login/login.htm?destination=https://my.example.com/someotherpage";
$agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)";
$cookie_file_path = "tmp/"; 

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$POSTFIELDS);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_REFERER, $reffer);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$result = curl_exec($ch);

curl_close($ch);
Now, I can pass the username and password, and it will redirect me to the form (https://my.example.com/someotherpage), but I can't figure out how to post to that form.

Any ideas?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

You can make as many curl calls as you want using a single curl handle.

Code: Select all

$url = "https://my.example.com/authentication.php";
$POSTFIELDS = "username=UNAME&password=PW&destination=https://my.example.com/someotherpage";
$reffer = "https://my.example.com/login/login.htm?destination=https://my.example.com/someotherpage";
$agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)";
$cookie_file_path = "tmp/";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$POSTFIELDS);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_REFERER, $reffer);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$result = curl_exec($ch);

$url = "https://my.example.com/someotherpage";
$POSTFIELDS = "someothervar=someothervalue";

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$POSTFIELDS);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$result2 = curl_exec($ch);
I hope that is what your after
Post Reply