如何使用 PHP 发送 POST 请求?
实际上,我想阅读搜索查询完成后出现的内容。问题是 URL 只接受 POST
方法,并且不使用 GET
方法执行任何操作...
我必须借助 读取所有内容>domdocument
或 file_get_contents()
。有没有什么方法可以让我使用 POST
方法发送参数,然后通过 PHP
读取内容?
Actually I want to read the contents that come after the search query, when it is done. The problem is that the URL only accepts POST
methods, and it does not take any action with GET
method...
I have to read all contents with the help of domdocument
or file_get_contents()
. Is there any method that will let me send parameters with POST
method and then read the contents via PHP
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
对于那些只是摆弄 php 的人来说,下面的代码将解锁你。我已参考现有答案来创建此片段。为了更好地理解,我添加了代码注释。
For someone who is just tinkering with php then the below code will unblock you. I have referred existing answers to create this snippet. I have added code comments for better understanding.
我更喜欢这个:
用法示例:
I prefer this one:
Usage example:
无CURL方法:
有关该方法以及如何添加标头的更多信息,请参阅PHP手册,例如:
CURL-less method:
See the PHP manual for more information on the method and how to add headers, for example:
您可以使用 cURL:
You could use cURL:
我使用以下函数通过curl 发布数据。
$data
是要发布的字段数组(将使用http_build_query()
正确编码)。@Edward 提到,
http_build_query()
可能会被省略,因为curl 将正确编码传递给CURLOPT_POSTFIELDS
参数的数组,这是正确的,但请注意,在这种情况下,数据将是使用multipart/form-data
进行编码,这可能并不理想,因为某些端点希望使用application/x-www-form-urlencoded
对数据进行编码。当使用上面函数中的http_build_query()
时,数据将使用application/x-www-form-urlencoded
进行编码。I use the following function to post data using curl.
$data
is an array of fields to post (will be correctly encoded usinghttp_build_query()
).@Edward mentions that
http_build_query()
may be omitted since curl will correctly encode array passed toCURLOPT_POSTFIELDS
parameter, which is correct, but be advised that in this case the data will be encoded usingmultipart/form-data
and it may not be desirable as some endpoints expect data to be encoded usingapplication/x-www-form-urlencoded
. When usinghttp_build_query()
as in the function above, data will be encoded withapplication/x-www-form-urlencoded
.我建议您使用开源包 guzzle 它经过了全面的单元测试并使用最新的编码实践。
安装 Guzzle
转到项目文件夹中的命令行并输入以下命令(假设您已经有包管理器 composer 安装)。如果您需要有关如何安装 Composer 的帮助,您应该看看这里。
使用 Guzzle 发送 POST 请求
Guzzle 的使用非常简单,因为它使用轻量级面向对象的 API:
I recommend you to use the open-source package guzzle that is fully unit tested and uses the latest coding practices.
Installing Guzzle
Go to the command line in your project folder and type in the following command (assuming you already have the package manager composer installed). If you need help how to install Composer, you should have a look here.
Using Guzzle to send a POST request
The usage of Guzzle is very straight forward as it uses a light-weight object-oriented API:
我想添加一些关于Fred Tanrikut 基于curl 的答案的想法。我知道其中大部分已经写在上面的答案中,但我认为最好显示一个包含所有这些内容的答案。
这是我编写的基于curl发出HTTP-GET/POST/PUT/DELETE请求的类,仅涉及响应主体:
改进
请参阅: http://php.net/en/manual/function.curl- exec.php
使用示例
GET
POST
PUT
DELETE
测试
您还可以使用这个简单的类进行一些很酷的服务测试。
I'd like to add some thoughts about the curl-based answer of Fred Tanrikut. I know most of them are already written in the answers above, but I think it is a good idea to show an answer that includes all of them together.
Here is the class I wrote to make HTTP-GET/POST/PUT/DELETE requests based on curl, concerning just about the response body:
Improvements
See: http://php.net/en/manual/function.curl-exec.php
Example of usage
GET
POST
PUT
DELETE
Testing
You can also make some cool service tests by using this simple class.
如果您有机会使用 Wordpress 来开发您的应用程序(它实际上是一种获取授权、信息页面等的便捷方法,即使对于非常简单的东西也是如此),您可以使用以下代码片段:
它使用不同的方式来发出实际的 HTTP 请求,取决于网络服务器上可用的内容。有关更多详细信息,请参阅 HTTP API 文档。
如果您不想开发自定义主题或插件来启动 WordPress 引擎,您可以在 WordPress 根目录中的独立 PHP 文件中执行以下操作:
它不会显示任何主题或输出任何 HTML,只需 hack 即可使用 WordPress API!
If you by any chance are using Wordpress to develop your app (it's actually a convenient way to get authorization, info pages etc even for very simple stuff), you can use the following snippet:
It uses different ways of making the actual HTTP request, depending on what is available on the web server. For more details, see the HTTP API documentation.
If you don't want to develop a custom theme or plugin to start the Wordpress engine, you can just do the following in an isolated PHP file in the wordpress root:
It won't show any theme or output any HTML, just hack away with the Wordpress APIs!
如果您要这样做,还有另一种 CURL 方法。
一旦您了解了 PHP curl 扩展的工作方式(将各种标志与 setopt() 调用相结合),这就非常简单了。在此示例中,我有一个变量 $xml,它保存我准备发送的 XML - 我将把它的内容发布到示例的测试方法中。
首先我们初始化连接,然后使用 setopt() 设置一些选项。这些告诉 PHP 我们正在发出一个 post 请求,并且我们正在发送一些数据,提供数据。 CURLOPT_RETURNTRANSFER 标志告诉curl 将输出作为curl_exec 的返回值提供给我们,而不是输出它。然后我们进行调用并关闭连接 - 结果在 $response 中。
There's another CURL method if you are going that way.
This is pretty straightforward once you get your head around the way the PHP curl extension works, combining various flags with setopt() calls. In this example I've got a variable $xml which holds the XML I have prepared to send - I'm going to post the contents of that to example's test method.
First we initialised the connection, then we set some options using setopt(). These tell PHP that we are making a post request, and that we are sending some data with it, supplying the data. The CURLOPT_RETURNTRANSFER flag tells curl to give us the output as the return value of curl_exec rather than outputting it. Then we make the call and close the connection - the result is in $response.
上述无卷曲方法的另一种替代方法是使用本机stream< /em> 函数:
stream_context_create()
:<块引用>
创建并返回一个流上下文,其中包含选项预设中提供的任何选项。
stream_get_contents()
:<块引用>
与
file_get_contents()
相同,除了stream_get_contents()
对已打开的流资源进行操作并返回字符串中的剩余内容,最多 maxlength 个字节,并且从指定的偏移开始。带有这些的 POST 函数可以简单地像这样:
Another alternative of the curl-less method above is to use the native stream functions:
stream_context_create()
:stream_get_contents()
:A POST function with these can simply be like this:
这里只使用一个没有 cURL 的命令。超级简单。
Here is using just one command without cURL. Super simple.
使用
PHP
发送GET
或POST
请求的更好方法如下:代码取自官方文档 http://docs.php.net/manual/da/httprequest.send.php
The better way of sending
GET
orPOST
requests withPHP
is as below:The code is taken from official documentation here http://docs.php.net/manual/da/httprequest.send.php
根据主要答案,这是我使用的:
示例用法:
Based on the main answer, here is what I use:
Example usage:
我一直在寻找类似的问题并找到了更好的方法来做到这一点。所以就这样了。
您可以简单地将以下行放在重定向页面上(例如 page1.php)。
我需要它来重定向 REST API 调用的 POST 请求。该解决方案能够使用发布数据以及自定义标头值进行重定向。
这是参考链接。
I was looking for a similar problem and found a better approach of doing this. So here it goes.
You can simply put the following line on the redirection page (say page1.php).
I need this to redirect POST requests for REST API calls. This solution is able to redirect with post data as well as custom header values.
Here is the reference link.
尝试 PEAR 的 HTTP_Request2 包来轻松发送 POST 请求。或者,您可以使用 PHP 的curl 函数或使用 PHP 流上下文。
HTTP_Request2 还可以模拟服务器,这样您就可以轻松地对代码进行单元测试
Try PEAR's HTTP_Request2 package to easily send POST requests. Alternatively, you can use PHP's curl functions or use a PHP stream context.
HTTP_Request2 also makes it possible to mock out the server, so you can unit-test your code easily
[编辑]:请忽略,现在在 php 中不可用。
您还可以使用另外一个
单击此处了解详细信息
[Edit]: Please ignore, not available in php now.
There is one more which you can use
Click here for details
我创建了一个使用 JSON 请求帖子的函数:
I make a function to request a post using JSON:
最重要的答案对我不起作用。这是第一个完美运行的解决方案:
The top answers didn't work for me. This was the first solution that ran perfectly: