我似乎在尝试将表单变量提交到多个 url 时遇到了问题。我在下面的代码从表单中获取变量并将它们存储在 php 中,然后我试图将这些变量发送到某些 url(变量确实通过,我已经测试过了)。我听说 cUrl 是这样做的方法,但我不知道它的工作原因是否让我难以理解如何检索响应代码。
<?php
$name = $_POST['firstname'];
$email = $_POST['email'];
$src = $_POST['srcUrl'];
$ip= $_SERVER['SERVER_ADDR'];
$suDate = date('Y-m-d H:i:s');
$data = array(
"fn" => $name,
"src" => $src,
"em" => $email,
"ip" => $ip,
"signupDate" => $suDate
);
$data2 = array(
"firstname" => $name,
"email" => $email,
);
function post_to_url($url, $data) {
$fields = '';
foreach($data as $key => $value) {
$fields .= $key . '=' . $value . '&';
}
rtrim($fields, '&');
$post = curl_init();
curl_setopt($post, CURLOPT_URL, $url);
curl_setopt($post, CURLOPT_POST, count($data));
curl_setopt($post, CURLOPT_POSTFIELDS, $fields);
curl_setopt($post, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($post);
curl_close($post);
}
post_to_url("http://exampleURL.com/page.php", $data);
post_to_url("http://exampleURL2.com/cgi-bin/add.cgi", $data2);
print_r($result);
?>
任何帮助是极大的赞赏。谢谢