使用PHP中的CURL提交表单

时间:2014-03-17 18:48:24

标签: php curl

在过去的几天里,我一直在破坏我的大脑,想要获得一个PHP脚本来用PHP提交一个带有CURL的表单。

这是该网站的源代码:

<form name="webform" action="mailto.php" method="post" onsubmit="return validateform(webform);">
<input type="hidden" name="submit" value="1">
<input type="hidden" name="name1" value="244959">
<input type="hidden" name="name2" value="<? echo $dogname; ?>">
<tr>
<td><font face="verdana" size="-1">Your Name: </td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td><font face="verdana" size="-1">Your Email Address: </td>
<td><input type="text" name="email"></td>
</tr>
<tr>
<td><font face="verdana" size="-1">Confirm Your Email Address: </td>
<td><input type="text" name="confirm_email"></td>
</tr>
<tr>
<td valign="top"><font face="verdana" size="-1">Your Message:</td>
<td><textarea name="comments" rows="8" cols="35"></textarea></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><img src="captcha.php"></td>
</tr>
<tr>
<td><input type="text" name="vercode" /></td>
</tr>
<tr>
<td><input type="submit" class=button name="Send" value="submit">
<br>
<br></td>
</tr>
</form>
</table>

这是提交脚本的代码:

function postform($id){
grab_image('path to/captcha.php', 'captcha.jpg');

$image = 'captcha.jpg';

$client = new DeathByCaptcha_SocketClient("user", "password");


if ($captcha = $client->decode($image, 10)) {

echo $captcha['text'] . "\n";
$url = "path to page.php";

$data = array();
$data['submit']='1';
$data['name1']=$id;
$data['name2']="name2";
$data['name']='MyName';
$data['email']='my@email.com';
$data['confirm_email']='my@email.com';
$data['comments']='Success Posting This Form';
$data['vercode']=$captcha['text'];

$post_str = '';

foreach($data as $key=>$value){
$post_str .= $key.'='.urlencode($value).'&';
}

$post_str = substr($post_str, 0, -1);


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url); // set url to post to 
curl_setopt($ch, CURLOPT_POST, TRUE); // set POST method 
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_str);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,TRUE); // return into a variable 

$result = curl_exec($ch); // run the whole process
curl_close($ch); 
echo $result; 
}
}
postform("page number");

一旦我执行了代码,我就会回来: &#34;缺少安全代码。请回去再试一次。&#34;

现在几天都在修补这个问题,我对如何继续进行有点困惑。任何帮助都会非常感激!

1 个答案:

答案 0 :(得分:0)

Html页面:

<!Doctype Html>
<html>
<head>
<title></title>
<script src="/qlib/qforms.js"></script>
</head>

<script language="JavaScript">
// set the path to the qForms directory
qFormAPI.setLibraryPath("/qlib/");
// this loads all the default libraries
qFormAPI.include("*");

</script>
<h2><img src="../../templates/images/mail-to/email.gif" width="60" height="48" alt="email" /><br />
Email Breeder</h2>
<p>Use the form below to contact Bernese Mountain Dog, Breeder ID 244959 <? echo $to; ?>:
<p><font color=#FF0000 face=verdana size=-2><b><? echo $msg; ?></b></font>
<p>
<table>
<form name="webform" action="mail_curl.php" onsubmit="return validateform(webform);">
<input type="hidden" name="submit" value="1">
<input type="hidden" name="breederid" value="244959">
<input type="hidden" name="dogname" value="<? echo $dogname; ?>">
<tr>
<td><font face="verdana" size="-1">Your Name: </td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td><font face="verdana" size="-1">Your Email Address: </td>
<td><input type="text" name="email"></td>
</tr>
<tr>
<td><font face="verdana" size="-1">Confirm Your Email Address: </td>
<td><input type="text" name="confirm_email"></td>
</tr>
<tr>
<td valign="top"><font face="verdana" size="-1">Your Message:</td>
<td><textarea name="comments" rows="8" cols="35"></textarea></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><img src="captcha.php"></td>
</tr>
<tr>
<td><input type="text" name="vercode" /></td>
</tr>
<tr>
<td><input type="submit" class=button name="Send" value="Send Email to Breeder">
<br>
<br></td>
</tr>
</form>
</table>
<script language="JavaScript">
// initialize the qForm object
objForm = new qForm("webform");

// make these fields required
objForm.required("name, email, confirm_email, comments, vercode");
objForm.email.validateEmail();
objForm.email.validateSame("confirm_email");

</script>
</body>
</html>

mail_curl.php:

 <?php

$url = 'path to mailto.php';

 if(isset($_GET)){
 $breederid = $_GET['breederid'];
 $dogname= $_GET['dogname'];
 $name= $_GET['name'];
 $email= $_GET['email'];
 $confirm_email= $_GET['confirm_email'];
 $comments= $_GET['comments'];
 $vercode= $_GET['vercode'];


 $fields = array(
 'breederid ' =>     urlencode($breederid),
 'dogname' =>     urlencode($dogname),
 'name' =>     urlencode($name),
 'email' =>     urlencode($email),
 'confirm_email' =>     urlencode($confirm_email),
 'comments' =>     urlencode($comments),
 'vercode' =>     urlencode($vercode)   
 );

foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');

$ch = curl_init();

curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

$result = curl_exec($ch);

curl_close($ch);
}
else
{
echo 'No data to submit';
}
?>

mailto.php:

 <?php
 $breederid = $_POST['breederid'];
 $dogname= $_POST['dogname'];
 $name= $_POST['name'];
 $email= $_POST['email'];
 $confirm_email= $_POST['confirm_email'];
 $comments= $_POST['comments'];
 $vercode= $_POST['vercode'];

 //mysqli insert statement
 //$con is the mysqli database connection
 $result = mysqli_query($con,"INSERT INTO mails (breederid, dogname, name, email, comments, vercode) 
 VALUES ('$breederid', '$dogname', '$name', '$email', '$comments', '$vercode')");       

 if($result){
 echo 'Data was inserted successfully';
 } else {
 echo 'There was an error inserting the data';
 }

   ?>