通过 PHP/Curl 将 HTML 表单传递到 JSP 页面...传递未发生
我有一个 HTML 表单,正在提交到 PHP 页面。 PHP 页面需要验证验证码,然后将表单值传递到 JSP 页面。我无法控制 JSP 页面。验证码运行良好。我的 PHP 页面中出现了一些问题,因为当它加载 JSP 页面上的信息时,目标页面的 CSS 和标头未加载,并且表单数据未传递。我无法访问 JSP 页面。有什么想法吗?
顺便说一句,验证码验证工作正常,如果我将其直接传递到 JSP 页面,HTML 工作正常:
<?php
require_once('recaptchalib.php');
$privatekey = "privatekeyhere";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
// What happens when the CAPTCHA was entered incorrectly
die ("<p align='center'>The reCAPTCHA wasn't entered correctly. Please go back and try it again." .
"(reCAPTCHA said: " . $resp->error . ")</p>");
} else {
$ipaddress = $_SERVER["REMOTE_ADDR"];
$fname = $_POST['fname'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$h = curl_init();
curl_setopt($h, CURLOPT_URL, "//remote JSP page");
curl_setopt($h, CURLOPT_HEADER, true);
curl_setopt($h, CURLOPT_POST, true);
curl_setopt($h, CURLOPT_POSTFIELDS, array(
'fname' => '$fname',
'address' => '$address',
'city' => '$city',
'state' => '$state',
'zip' => '$zip',
'phone' => '$phone',
'email' => '$email',
));
curl_setopt($h, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($h);
echo $result;
}
?>
I have an HTML form, being submitted to a PHP page. The PHP page needs to validate a captcha and then pass the form values to a JSP page. I have NO control over the JSP page. Captcha is working beautifully. Something is getting lost in my PHP page, as when it loads the information on the JSP page, the CSS and headers of the target page aren't loading and the form data isn't being passed. I do not have access to the JSP page. Any ideas?
BTW, Captcha validation is working fine and the HTML works fine if I pass it directly to the JSP page:
<?php
require_once('recaptchalib.php');
$privatekey = "privatekeyhere";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
// What happens when the CAPTCHA was entered incorrectly
die ("<p align='center'>The reCAPTCHA wasn't entered correctly. Please go back and try it again." .
"(reCAPTCHA said: " . $resp->error . ")</p>");
} else {
$ipaddress = $_SERVER["REMOTE_ADDR"];
$fname = $_POST['fname'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$h = curl_init();
curl_setopt($h, CURLOPT_URL, "//remote JSP page");
curl_setopt($h, CURLOPT_HEADER, true);
curl_setopt($h, CURLOPT_POST, true);
curl_setopt($h, CURLOPT_POSTFIELDS, array(
'fname' => '$fname',
'address' => '$address',
'city' => '$city',
'state' => '$state',
'zip' => '$zip',
'phone' => '$phone',
'email' => '$email',
));
curl_setopt($h, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($h);
echo $result;
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可以立即发现的问题:
CURLOPT_POSTFIELD
必须是 POST 字符串,而不是数组。对于第 1 点,这里有一个由 David Walsh 编写的简单 cUrl 教程,涵盖了与您的情况非常相似的情况,并包含一种在 POST 字符串中转换数组的方法: http://davidwalsh.name/execute-http-post-php-curl
对于点2、
'singlequotes'
内的字符串不会被计算。要引用变量,您应该使用$var
,而不是'$var'
。 奖励:POST/GET 字符串中的任何内容都应该进行 urlencode 编码。
Problems I can spot right away:
CURLOPT_POSTFIELD
has to be a POST string, not an array.For point 1, here's a simple cUrl tutorial by David Walsh, covering a case very similar to yours and containing a way to transform arrays in POST strings: http://davidwalsh.name/execute-http-post-php-curl
For point 2, strings inside
'single quotes'
are not evaluated. To refer to a variable, you should use$var
, not'$var'
.Bonus: anything in a POST/GET string should be urlencoded.