发布到 PHP 脚本中的另一个页面 [英] Post to another page within a PHP script

查看:35
本文介绍了发布到 PHP 脚本中的另一个页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 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?

推荐答案

让 PHP 执行 POST 请求的最简单方法可能是使用 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);

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

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 - 好吧,我已经有一段时间没写了.现在值得检查 Guzzle,它在使用或不使用 curl 扩展时都可以正常工作.

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.

这篇关于发布到 PHP 脚本中的另一个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆