Jump to content

I need help with cURL. Submitting data like a html form.


Jarid

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

<?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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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