一次发送$ _POST到2个php文件

时间:2012-05-31 16:50:10

标签: php post

我不知道如何解决发送$ _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);

  ?>

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

foo.php

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

注意:您需要在allow_url_fopen文件中启用php.ini

答案 2 :(得分:0)

你需要用javascript ajax做一个POST。

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

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

$(selector).post(url,data,success(response,status,xhr),dataType)
相关问题