MerlinSB1 Posted May 3, 2012 Share Posted May 3, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/262008-curl-error/ Share on other sites More sharing options...
MMDE Posted May 3, 2012 Share Posted May 3, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/262008-curl-error/#findComment-1342672 Share on other sites More sharing options...
Maq Posted May 3, 2012 Share Posted May 3, 2012 Merlin, in the future, place OR tags around your code. Quote Link to comment https://forums.phpfreaks.com/topic/262008-curl-error/#findComment-1342717 Share on other sites More sharing options...
MerlinSB1 Posted May 3, 2012 Author Share Posted May 3, 2012 Thanks both - solved it. Quote Link to comment https://forums.phpfreaks.com/topic/262008-curl-error/#findComment-1342747 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.