Jump to content

Recommended Posts

Hi,

 

I have used cURL to login in to a site and successful passed and stored the cookie, but I can't get it to submit a form on another page. When I echo the result it is just blank - although I can retrieve other pages fine.

 

I think the page is trying to redirect, which is fine I just want to see if it has submitted or not! In case its useful the form is self referencing and was built using MOOJ Proforms (by some one else).

 

Any ideas?

 

$post_data['m4j-180'] = 'Yes';
$post_data['m4j-181'] = 'No';
$post_data['m4j-182'] = 'Yes';
$post_data['m4j-170'] = 'This is a test';
$post_data['submit'] = 'send';

//turn key pairs into strings and combine them (key1=value1&key2=value2)

foreach ($post_data as $key => $value) {
$post_items[] = $key . '=' . $value;
}
$post_string = implode ('&', $post_items);

//Open connection -- address is the "action" setting of the form
$curl_connection = curl_init('http://www.testsite.com/index.php?option=com_proforms&jid=6&cid=-1&Itemid=54');

//Set connection options

//Set connection to timeout after 30 secs so script doesn't hang
curl_setopt($curl_connection, CURLOPT_CONNCTTIMEOUT, 180);

//Tell destination URL to output browser content not mobile etc
curl_setopt($curl_connection, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");

//Tells curl to out results as string not as a display
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);

//false below means no error will trigger if SSL not signed or out of date
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);

//follow location redirects in header of destination file
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, true);

//tell curl which string to post

curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);

//execute the post

$result = curl_exec($curl_connection);

//see any errors that might have happened

print_r(curl_getinfo($curl_connection));

//see the error number and description

echo curl_errno($curl_connection) . '-' . curl_error($curl_connection);

//close connection
curl_close($curl_connection);

echo $result

Link to comment
https://forums.phpfreaks.com/topic/262008-curl-error/
Share on other sites

Heya

 

I think your problem is that you need to specify the length of the post data, or with other words how many fields you are submitting. You do that with CURLOPT_POST.

 

curl_setopt($curl_connection, CURLOPT_POST, preg_match('/=/',$post_string));

^ of course, if there is any extra equals in the string, it won't work very well, but seeing as your current script doesn't assume this either, I'm thinking it's okay! ;)

curl_setopt($curl_connection, CURLOPT_POST, count($post_data));

^ can also be used.

 

You may want to read this post:

http://www.phpfreaks.com/forums/index.php?topic=357492.msg1689951#msg1689951

Link to comment
https://forums.phpfreaks.com/topic/262008-curl-error/#findComment-1342672
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.