0

我不知道如何解决发送 $_POST 的问题。我想在 example.com 上填写表格

//at example.com
<form action="foo.php" method="post" >
<input name="bar1" type="text" />
<input name="bar2" type="text" />
<input name="bar3" type="text" />
<input value="Send" type="submit" />
</form>

然后转到 foo.php :

<?php //foo.php
echo 'added: <p>'.$_POST['bar1'].'<br />'.$_POST['bar2'].'<br />'.$_POST['bar3']; 
?>

同时它也发送

$_POST['bar1'], $_POST['bar2'], $_POST['bar3']

到 exampledomain.com/foobar.php 可以将其保存到文件中 - 这不是问题。

我不知道如何同时向两个 php 脚本发送信息 - 一个是外部脚本。我想我必须以某种方式在 foo.php 中发送它

有一种解决方案 - 重定向到 foo.php 内的 exampledomain.com/foobar.php 但在我的情况下这是不可接受的 - 我想在不让用户退出 example.com 的情况下这样做

在此先感谢您,希望您能理解我的问题-如果不只是提出评论

编辑:基于 Pete Herbert Penito 的回答:

 <?php //inside foo.php
 $url = 'http://exampledomain.com/foobar.php';
 $fields_string='';
 foreach($_POST as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
 rtrim($fields_string,'&');

 //open connection
 $ch = curl_init();

 //set the url, number of POST vars, POST data
 curl_setopt($ch,CURLOPT_URL,$url);
 curl_setopt($ch,CURLOPT_POST,count($_POST));
 curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

 //execute post
 $result = curl_exec($ch);

 //close connection
 curl_close($ch);

  ?>
4

3 回答 3

2

我会使用 CURL 来构造一个发布请求:

<?php
 // these variables would need to be changed to be your variables 
 // alternatively you could send the entire post constructed using a foreach
 if(isset($_POST['Name']))     $Name   = $_POST['Name'];
 if(isset($_POST['Email']))   $Email   = $_POST['Email'];
 if(isset($_POST['Message']))   $Message= htmlentities($_POST['Message']);

 $Curl_Session = curl_init('http://www.site.com/cgi-bin/waiting.php');
 curl_setopt ($Curl_Session, CURLOPT_POST, 1);
 curl_setopt ($Curl_Session, CURLOPT_POSTFIELDS, "Name=$Name&Email=$Email&Message=$Message");
 curl_setopt ($Curl_Session, CURLOPT_FOLLOWLOCATION, 1);
 curl_exec ($Curl_Session);
 curl_close ($Curl_Session);
?>

来自链接:

http://www.askapache.com/php/sending-post-form-data-php-curl.html

于 2012-05-31T16:54:19.583 回答
1

在你的foo.php

<?php
include 'http://exampledomain.com/foobar.php';

注意:您需要allow_url_fopenphp.ini文件中启用。

于 2012-05-31T16:51:44.280 回答
0

您将需要使用 javascript ajax 执行 POST 之一。

然后是真正的帖子,它将像往常一样重定向浏览器。

http://www.w3schools.com/jquery/ajax_post.asp

$(selector).post(url,data,success(response,status,xhr),dataType)
于 2012-05-31T16:57:06.463 回答