我当前的主机不允许远程 mysql 访问,因此我需要通过让 server1 上的 script1 与 server2 上的 script2 通信来解决这个问题。我正在尝试将发布数据发送到 script2,然后 script2 获取该数据并将其放入 mysql 中。为了让我的问题简单,我将代码简化为:
脚本1
for ( $counter = $counternumbernao; $counter <= $amountofcomments; $counter += 1)
{
echo'
<form action="http://server2.x.com/form-receive.php" method="post">
<INPUT TYPE=HIDDEN NAME="comment_content" value=$comment_content>
<INPUT TYPE=HIDDEN NAME="comment_date" value=$comment_date">
<input type="submit" />
</form>
';
}
我怎样才能改变这段代码,以便每次循环发生时它都会自动将$_POST数据发送到script2,然后将其放入mysql?我认为没有必要包含 script2,因为它对这个问题并不重要。
最佳答案
为什么不使用 cURL 将 FORM 数据发送到 server2。它允许您发送 HTTP 请求。尝试以下脚本:
$url = "http://server2.x.com/form-receive.php";
$postvars = "comment_content=$comment_content&comment_date=$comment_date";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST ,1);
curl_setopt($ch, CURLOPT_POSTFIELDS ,$postvars);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION ,1);
curl_setopt($ch, CURLOPT_HEADER ,0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER ,1);
$data = curl_exec($ch);
关于php - 如何在循环中 $_POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6452294/