Jarid Posted January 3, 2011 Share Posted January 3, 2011 I have looked up tutorials on Google. I am still confused. I built a php script that receives the data from my html form then filters out bad words. I am trying to get the script to resend the data to pm.cgi. The form was originally targeting pm.cgi but it needed filtered. This is why i am using php in the first place. Please help me pass these variables from the php script to the cgi script the same way that a html form would. Thank you. Here is my code, it prints correctly. I cannot get cURL to work. <?php $action = $_GET['action'] ; $login = $_REQUEST['login'] ; $ID = $_REQUEST['ID'] ; $text = $_REQUEST['Unused10'] ; $submit = $_REQUEST['submit'] ; function filterBadWords($str){ // words to filter $badwords=array( "badword1", "badword2"); // replace filtered words with random spaces $replacements=array( "****", "***", "****" ); for($i=0;$i < sizeof($badwords);$i++){ srand((double)microtime()*1000000); $rand_key = (rand()%sizeof($replacements)); $str=eregi_replace($badwords[$i], $replacements[$rand_key], $str); } return $str; } $text = filterBadWords($text); print_r($action); print_r($login); print_r($ID); print_r($text); print_r($submit); ?> I need to send the variables that i printed above to pm.cgi. Can you point me in the right direction? Thanks again! Link to comment https://forums.phpfreaks.com/topic/223280-i-need-help-with-curl-submitting-data-like-a-html-form/ Share on other sites More sharing options...
BlueSkyIS Posted January 3, 2011 Share Posted January 3, 2011 what do you mean by "I cannot get cURL to work"? What doesn't work? Are there errors? I don't see any curl code in the snippet you posted. Does pm.cgi take POST or GET? What are the form field names that pm.cgi expects? Link to comment https://forums.phpfreaks.com/topic/223280-i-need-help-with-curl-submitting-data-like-a-html-form/#findComment-1154279 Share on other sites More sharing options...
johnny86 Posted January 3, 2011 Share Posted January 3, 2011 I agree that cURL would be the simplest way to do that. This is just a direction I would go: <?php $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, 'http://your.cgi.script/'); curl_setopt($curl, CURLOPT_HEADER, true); // Make basic post request curl_setopt($curl, CURLOPT_POST, true); // Set your post data $data = array( 'action' => 'something', 'id' => 'something_else', 'login' => 'more' ); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); curl_exec($ch); curl_close($ch); ?> Multidimensional arrays will not work. If you have such data you should serialize the data to string and then unserialize at the other end. Hope this helps. Link to comment https://forums.phpfreaks.com/topic/223280-i-need-help-with-curl-submitting-data-like-a-html-form/#findComment-1154285 Share on other sites More sharing options...
Jarid Posted January 3, 2011 Author Share Posted January 3, 2011 The pm.cgi script takes post. <?php $action = $_GET['action'] ; $login = $_REQUEST['login'] ; $ID = $_REQUEST['ID'] ; $text = $_REQUEST['Unused10'] ; $submit = $_REQUEST['submit'] ; function filterBadWords($str){ // words to filter $badwords=array( "badword1", "badword2"); // replace filtered words with random spaces $replacements=array( "***", "***"); for($i=0;$i < sizeof($badwords);$i++){ srand((double)microtime()*1000000); $rand_key = (rand()%sizeof($replacements)); $str=eregi_replace($badwords[$i], $replacements[$rand_key], $str); } return $str; } $text = filterBadWords($text); print("Hello World"); print_r($action); print_r($login); print_r($ID); print_r($text); print_r($submit); $curl_connection = curl_init('http://johnstonstudentcenter.com/members/cgi/pm.cgi'); curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30); curl_setopt($curl_connection, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"); curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1); $post_data['action'] = 'modified'; $post_data['login'] = '$login'; $post_data['ID'] = '$ID'; $post_data['Unused10'] = '$text'; $post_data['submit'] = ' Update '; $result = curl_exec($curl_connection); curl_close($curl_connection); ?> Link to comment https://forums.phpfreaks.com/topic/223280-i-need-help-with-curl-submitting-data-like-a-html-form/#findComment-1154289 Share on other sites More sharing options...
Jarid Posted January 3, 2011 Author Share Posted January 3, 2011 Im going to try your way johnny it makes more sense then what i just came up with. Thanks for the reply Link to comment https://forums.phpfreaks.com/topic/223280-i-need-help-with-curl-submitting-data-like-a-html-form/#findComment-1154292 Share on other sites More sharing options...
BlueSkyIS Posted January 3, 2011 Share Posted January 3, 2011 do not single-quote strings that should be replaced by a value. single quotes cause the string to be interpreted literally. $post_data['ID'] = $ID; $post_data['login'] = $login; $post_data['Unused10'] = $text; $post_data['login'] = '$login'; $post_data['ID'] = '$ID'; $post_data['Unused10'] = '$text'; echo $post_data['login'] . "<br />"; echo $post_data['ID'] . "<br />"; echo $post_data['Unused10'] . "<br />"; output: $login $ID $text Link to comment https://forums.phpfreaks.com/topic/223280-i-need-help-with-curl-submitting-data-like-a-html-form/#findComment-1154295 Share on other sites More sharing options...
Jarid Posted January 3, 2011 Author Share Posted January 3, 2011 <?php $action = $_GET['action'] ; $login = $_REQUEST['login'] ; $ID = $_REQUEST['ID'] ; $text = $_REQUEST['Unused10'] ; $submit = $_REQUEST['submit'] ; function filterBadWords($str){ // words to filter $badwords=array( "badword1", "badword2"); // replace filtered words with random spaces $replacements=array( "****", "***", "****" ); for($i=0;$i < sizeof($badwords);$i++){ srand((double)microtime()*1000000); $rand_key = (rand()%sizeof($replacements)); $str=eregi_replace($badwords[$i], $replacements[$rand_key], $str); } return $str; } $text = filterBadWords($text); print("Hello World"); print_r($action); print_r($login); print_r($ID); print_r($text); print_r($submit); $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, 'http:johnstonstudentcenter.com/members/cgi/pm.cgi'); curl_setopt($curl, CURLOPT_HEADER, true); // Make basic post request curl_setopt($curl, CURLOPT_POST, true); // Set your post data $post_data['action'] = '$action'; $post_data['login'] = '$login'; $post_data['ID'] = '$ID'; $post_data['Unused10'] = '$text'; $post_data['submit'] = '$submit'; curl_setopt($curl, CURLOPT_POSTFIELDS, $data); curl_exec($ch); curl_close($ch); ?> Alright here are the errors: Warning: curl_exec(): supplied argument is not a valid cURL handle resource in /home/content/72/4744872/html/members/cgi/filter.php on line 50 Warning: curl_close(): supplied argument is not a valid cURL handle resource in /home/content/72/4744872/html/members/cgi/filter.php on line 51 Im guessing that i didn't group these together correctly. $post_data['action'] = 'modified'; $post_data['login'] = '$login'; $post_data['ID'] = '$ID'; $post_data['Unused10'] = '$text'; $post_data['submit'] = ' Update '; I also tried johnny's way with the same errors Link to comment https://forums.phpfreaks.com/topic/223280-i-need-help-with-curl-submitting-data-like-a-html-form/#findComment-1154300 Share on other sites More sharing options...
johnny86 Posted January 3, 2011 Share Posted January 3, 2011 Change the $ch in those statements to $curl. =) Link to comment https://forums.phpfreaks.com/topic/223280-i-need-help-with-curl-submitting-data-like-a-html-form/#findComment-1154306 Share on other sites More sharing options...
Jarid Posted January 3, 2011 Author Share Posted January 3, 2011 On the code that i posted or the code that you posed? Link to comment https://forums.phpfreaks.com/topic/223280-i-need-help-with-curl-submitting-data-like-a-html-form/#findComment-1154307 Share on other sites More sharing options...
johnny86 Posted January 3, 2011 Share Posted January 3, 2011 The code in your last post, which was what I suggested the first time. The curl handle is not set to $ch its $curl. So it needs to be $curl in the lines too. Let me know what happens then =) Link to comment https://forums.phpfreaks.com/topic/223280-i-need-help-with-curl-submitting-data-like-a-html-form/#findComment-1154310 Share on other sites More sharing options...
Jarid Posted January 3, 2011 Author Share Posted January 3, 2011 No errors in the php... But the pm.cgi script is not receiving it correctly. both of these are working without error $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, 'http:johnstonstudentcenter.com/members/cgi/pm.cgi'); curl_setopt($curl, CURLOPT_HEADER, true); // Make basic post request curl_setopt($curl, CURLOPT_POST, true); // Set your post data $post_data['action'] = 'modified'; $post_data['login'] = '$login'; $post_data['ID'] = '$ID'; $post_data['Unused10'] = '$text'; $post_data['submit'] = ' Update '; curl_setopt($curl, CURLOPT_POSTFIELDS, $data); curl_exec($curl); curl_close($curl); ?> and $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, 'http:johnstonstudentcenter.com/members/cgi/pm.cgi'); curl_setopt($curl, CURLOPT_HEADER, true); // Make basic post request curl_setopt($curl, CURLOPT_POST, true); // Set your post data $data = array( 'action' => 'modified', 'login' => '$login', 'ID' => '$ID', 'Unused10' => '$text', 'submit' => ' Update ' ); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); curl_exec($curl); curl_close($curl); ?> here is the original html form. <form action=filter.php method=post ENCTYPE="multipart/form-data" target="_blank"> <input type=hidden name=action value=modified> <input type=hidden name=login value="%%login%%"> <input type=hidden name=ID value="%%ID%%"> <textarea cols=28 rows=4 name=Unused10 MAXLENGTH="500">%%Unused10%%</textarea> <input type=submit name=submit value=" Update "> </form> Any ideas? Quick note: the cgi script works correctly when the data is coming straight from this form. Link to comment https://forums.phpfreaks.com/topic/223280-i-need-help-with-curl-submitting-data-like-a-html-form/#findComment-1154315 Share on other sites More sharing options...
johnny86 Posted January 3, 2011 Share Posted January 3, 2011 Yeah, you have declared enctype="multipart/form-data" in your form. By default POST uses application/x-www-form-urlencoded which is how cURL is also trying to send the data by default. That might be the problem why your pm.cgi is receiving incorrect data. You can tell your server that it is multipart/form-data by sending a header: Content-type: multipart/form-data Or just change your html form enctype to application/x-www-form-urlencoded Link to comment https://forums.phpfreaks.com/topic/223280-i-need-help-with-curl-submitting-data-like-a-html-form/#findComment-1154317 Share on other sites More sharing options...
Jarid Posted January 3, 2011 Author Share Posted January 3, 2011 i changed the html form to application/x-www-form-urlencoded no errors, but the cgi script still didn't receive and process the data. I had the cgi script export an example form for me and it had the form set to multipart/form-data... so it might require that for the modify data action. how do i change the php script for multipart/form-data? Link to comment https://forums.phpfreaks.com/topic/223280-i-need-help-with-curl-submitting-data-like-a-html-form/#findComment-1154327 Share on other sites More sharing options...
johnny86 Posted January 3, 2011 Share Posted January 3, 2011 What does the cgi script receive? Something missing? Data is all messed up? Try to print out everything what the CGI script receives. I'd like to see some of that data myself aswell to help me understand what happens during the data is passed. =) Link to comment https://forums.phpfreaks.com/topic/223280-i-need-help-with-curl-submitting-data-like-a-html-form/#findComment-1154331 Share on other sites More sharing options...
Jarid Posted January 3, 2011 Author Share Posted January 3, 2011 Ah Crap... The cgi script is a large membership management script....... And i have no idea how it works. Even worse it does not have an error log.... Here is what im thinking about doing. Using the script above to filter the $text. Next ill fill the form with the php variables then resubmit it using javascript. Since all of this happens in a hidden i frame its not really a big deal. Do you think this will work? Link to comment https://forums.phpfreaks.com/topic/223280-i-need-help-with-curl-submitting-data-like-a-html-form/#findComment-1154335 Share on other sites More sharing options...
johnny86 Posted January 3, 2011 Share Posted January 3, 2011 Oh and you can try removing the curl_setopt(CURLOPT_POST) line and adding your own headers $headers = array( 'Content-Type: multipart/form-data' // And more if needed ); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); from php.net manual to remember: dont do this without making sure server supports the custom request method IFrame would work fine. I often use iframes to upload files to server and it appears that no page was loaded at all. You must know what to send to the cgi script tough? What the cgi script expects to receive? Link to comment https://forums.phpfreaks.com/topic/223280-i-need-help-with-curl-submitting-data-like-a-html-form/#findComment-1154337 Share on other sites More sharing options...
Jarid Posted January 3, 2011 Author Share Posted January 3, 2011 The script expects to receive: Action Login ID In this case the action is set to modify so it also expects to receive another field with the same name as a db field. I am going to convert my variables to javascript refill the html form, and then submit it with a javascript. Thanks for the help, I'm a php noob. Link to comment https://forums.phpfreaks.com/topic/223280-i-need-help-with-curl-submitting-data-like-a-html-form/#findComment-1154344 Share on other sites More sharing options...
Jarid Posted January 3, 2011 Author Share Posted January 3, 2011 I got it working. It only took 3 languages lol. Link to comment https://forums.phpfreaks.com/topic/223280-i-need-help-with-curl-submitting-data-like-a-html-form/#findComment-1154354 Share on other sites More sharing options...
johnny86 Posted January 3, 2011 Share Posted January 3, 2011 Good job =)) Did you use PHP for something? Link to comment https://forums.phpfreaks.com/topic/223280-i-need-help-with-curl-submitting-data-like-a-html-form/#findComment-1154357 Share on other sites More sharing options...
Jarid Posted January 3, 2011 Author Share Posted January 3, 2011 Yep filtering out words and setting vars in javascript. Link to comment https://forums.phpfreaks.com/topic/223280-i-need-help-with-curl-submitting-data-like-a-html-form/#findComment-1154366 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.