在 PHP 脚本中发布到另一个页面

发布于 2024-07-30 02:35:53 字数 187 浏览 4 评论 0原文

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

How can I make a post request to a different php page within a php script? I have one front end computer as the html page server, but when the user clicks a button, I want a backend server to do the processing and then send the information back to the front end server to show the user. I was saying that I can have a php page on the back end computer and it will send the information back to the front end. So once again, how can I do a POST request to another php page, from a php page?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

倾城月光淡如水﹏ 2024-08-06 02:35:53

让 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,它再次可以在有或没有curl扩展的情况下工作。

Possibly the easiest way to make PHP perform a POST request is to use cURL, either as an extension or simply shelling out to another process. Here's a post sample:

// 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);

Also check out Zend_Http set of classes in the Zend framework, which provides a pretty capable HTTP client written directly in PHP (no extensions required).

2014 EDIT - well, it's been a while since I wrote that. These days it's worth checking Guzzle which again can work with or without the curl extension.

初相遇 2024-08-06 02:35:53

假设您的 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 手册

Assuming your php install has the CURL extension, it is probably the easiest way (and most complete, if you wish).

Sample snippet:

//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);

Credits go to http://php.dzone.com.
Also, don't forget to visit the appropriate page(s) in the PHP Manual

烟火散人牵绊 2024-08-06 02:35:53

索引.php

$url = 'http://[host]/test.php';
$json = json_encode(['name' => 'Jhonn', 'phone' => '128000000000']);

$options = ['http' => [
    'method' => 'POST',
    'header' => 'Content-type:application/json',
    'content' => $json
]];

$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);

测试.php

$raw = file_get_contents('php://input');
$data = json_decode($raw, true);
echo $data['name']; // Jhonn

index.php

$url = 'http://[host]/test.php';
$json = json_encode(['name' => 'Jhonn', 'phone' => '128000000000']);

$options = ['http' => [
    'method' => 'POST',
    'header' => 'Content-type:application/json',
    'content' => $json
]];

$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);

test.php

$raw = file_get_contents('php://input');
$data = json_decode($raw, true);
echo $data['name']; // Jhonn
难理解 2024-08-06 02:35:53

对于 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 扩展

For PHP processing, look into cURL. It will allow you to call pages on your back end and retrieve data from it. Basically you would do something like this:

$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);

You can also look into the PHP HTTP Extension.

孤独岁月 2024-08-06 02:35:53
  1. 正如其他用户所说,使用 CURL 执行此操作最简单。

  2. 如果curl不适合你那么也许
    http://netevil.org/blog/2006/ nov/http-post-from-php-without-curl

  3. 如果不可能,你可以自己编写套接字
    http://petewarden.typepad.com/searchbrowser/ 2008/06/how-to-post-an.html

  1. Like the rest of the users say it is easiest to do this with CURL.

  2. If curl isn't available for you then maybe
    http://netevil.org/blog/2006/nov/http-post-from-php-without-curl

  3. If that isn't possible you could write sockets yourself
    http://petewarden.typepad.com/searchbrowser/2008/06/how-to-post-an.html

朱染 2024-08-06 02:35:53

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

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

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

For those using cURL, note that CURLOPT_POST option is taken as a boolean value, so there's actually no need to set it to the number of fields you are POSTing.
Setting CURLOPT_POST to TRUE (i.e. any integer except zero) will just tell cURL to encode the data as application/x-www-form-urlencoded, although I bet this is not strictly necessary when you're passing a urlencoded string as CURLOPT_POSTFIELDS, since cURL should already tell the encoding by the type of the value (string vs array) which this latter option is set to.

Also note that, since PHP 5, you can use the http_build_query function to make PHP urlencode the fields array for you, like this:

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
烟花肆意 2024-08-06 02:35:53

解决方案在 target="_blank" 中,如下所示:

http://www.ozzu.com/website-design-forum/multiple-form-submit-actions-t25024.html

编辑表单,如下所示:

并使用此脚本:

    <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>

Solution is in target="_blank" like this:

http://www.ozzu.com/website-design-forum/multiple-form-submit-actions-t25024.html

edit form like this:

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

And use this script:

    <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>
丢了幸福的猪 2024-08-06 02:35:53

虽然不理想,但如果 cURL 选项不能为您做到这一点,可以尝试使用 shell_exec();

Although not ideal, if the cURL option doesn't do it for you, may be try using shell_exec();

失退 2024-08-06 02:35:53

CURL 方法非常流行,所以使用它是很好的。 您还可以通过一些额外的注释来解释更多这些代码,因为初学者可以理解它们。

CURL method is very popular so yes it is good to use it. You could also explain more those codes with some extra comments because starters could understand them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文