如何在php脚本中向不同的php页面发送帖子请求?我有一台前端计算机作为html页面服务器,但当用户单击一个按钮时,我希望后端服务器进行处理,然后将信息发送回前端服务器以显示用户。我说我可以在后端计算机上有一个php页面,它会将信息发送回前端。再一次,如何从php页面向另一个php页面发出POST请求?

有帮助吗?

解决方案

使PHP执行POST请求的最简单方法是使用 cURL ,作为扩展名或简单地炮轰到另一个进程。这是一个帖子样本:

// where are we posting to?
$url = 'http://foo.com/script.php';

// what post fields?
$fields = array(
   'field1' => $field1,
   'field2' => $field2,
);

// build the urlencoded data
$postvars = http_build_query($fields);

// 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($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);

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

// close connection
curl_close($ch);

另请查看Zend框架中的 Zend_Http 类集,它提供了一个非常强大的HTTP客户端,直接用PHP编写(无需扩展)。

2014年编辑 - 好吧,自从我写这篇文章以来已经有一段时间了。这些天值得检查 Guzzle ,它可以在有或没有卷曲扩展的情况下工作。

其他提示

假设您的php安装具有CURL扩展名,它可能是最简单的方法(如果您愿意,最完整的方式)。

示例代码段:

//set POST variables
$url = 'http://domain.com/get-post.php';
$fields = array(
                      'lname'=>urlencode($last_name),
                      'fname'=>urlencode($first_name),
                      'email'=>urlencode($email)
               );

//url-ify the data for the POST
foreach($fields 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($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

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

//close connection
curl_close($ch);

致谢 http://php.dzone.com 。 此外,请不要忘记访问相应的页面 PHP手册

对于PHP处理,请查看 cURL 。它允许您调用后端的页面并从中检索数据。基本上你会做这样的事情:

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_URL,$fetch_url);
curl_setopt ($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt ($ch,CURLOPT_USERAGENT, $user_agent;
curl_setopt ($ch,CURLOPT_CONNECTTIMEOUT,60);
$response = curl_exec ( $ch );
curl_close($ch);

您还可以查看 PHP HTTP扩展

  1. 与其他用户一样,使用CURL最容易做到这一点。

  2. 如果没有卷曲,那么也许 http://netevil.org/blog/2006/月/ HTTP-POST从 - PHP-无卷曲

  3. 如果不可能,您可以自己编写套接字 http://petewarden.typepad.com/searchbrowser/ 2008/06 /如何对后an.html

对于那些使用cURL的人,请注意CURLOPT_POST选项被视为布尔值,因此实际上不需要将其设置为您正在POST的字段数。
将CURLOPT_POST设置为TRUE(即除零之外的任何整数)将告诉cURL将数据编码为application / x-www-form-urlencoded,尽管我打赌当你将urlencoded字符串作为CURLOPT_POSTFIELDS传递时,这并不是绝对必要的,因为cURL应该已经通过后一个选项设置的值的类型(字符串与数组)来告诉编码。

另请注意,从PHP 5开始,您可以使用http_build_query函数为您的PHP urlencode创建fields数组,如下所示:

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));

解决方案在target = <!> quot; _blank <!>中;像这样:

http://www.ozzu。 COM /网站设计论坛/多形式提交 - 行动 - t25024.html

编辑这样的表格:

<form method="post" action="../booking/step1.php" onsubmit="doubleSubmit(this)">

并使用此脚本:

    <script type="text/javascript">
<!--
function doubleSubmit(f)
{
  // submit to action in form
  f.submit();
  // set second action and submit
  f.target="_blank";
  f.action="../booking/vytvor.php";
  f.submit();
  return false;
}
//-->
</script>

虽然不理想,如果cURL选项不适合你,可以尝试使用shell_exec();

CURL方法很受欢迎,所以使用它是好的。您还可以通过一些额外的评论来解释这些代码,因为初学者可以理解它们。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top