【发布时间】:2017-07-26 23:29:43
【问题描述】:
我有一个通过 POST 提交的表单,并在提交表单后捕获变量。
如何连接表单数据,然后将其发布到 url,然后重定向到感谢页面?
这不是确切的代码,我只是找不到任何正常的答案,而且我确信有不止一种方法可以做到这一点。只是想找出最简单的方法。
if(isset($_POST['submit'])){
$var1 = $_POST['var1'];
$var2 = $_POST['var2'];
$url = 'https://api.this.com/foo/bar?token=IHAVETOKEN&foo=$Var1&bar=$var2'
post_request_to($url);
header("Location: thankyou.php");
}
编辑:
这是实际的答案/工作代码:
if(isset($_GET['submit'])){
$firstName = $_GET['firstname'];
$lastName = $_GET['lastname'];
$email = $_GET['email'];
$password = $_GET['password'];
$phone = $_GET['phone'];
}
$data = array(
'token' => 'sadfhjka;sdfhj;asdfjh;hadfshj',
'firstName' => $firstName,
'lastName' => $lastName,
'email' => $email,
'password' => $password,
'phone' => $phone
);
$postvars = http_build_query($data) . "\n";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.com/foo/bar?');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
【问题讨论】:
-
您意识到您发布的内容存在解析错误。另外,变量不会用单引号解析。
-
这不是确切的代码。我找不到一个通用的方法来做到这一点。
-
另外,变量区分大小写。您的代码在很多级别上都失败了。
-
欢迎来到 StackOverflow。请阅读How to Ask 和minimal reproducible example 然后编辑您的问题。
-
这有点太宽泛了。为什么不把你的伪代码变成真正的代码,如果它不起作用或者可能会回来。我认为 SO 社区在这里很慷慨,对于这么多答案来说,这确实是一个广泛的问题。