如何使用 PHP 捕获完整的 HTTP 请求数据(标头和正文)?
我在实现适用于 Java 的 API 时遇到问题,但无法使用 cURL.到目前为止,我们已经完成了所有工作,Java 发出的请求与我们发出的请求之间肯定有一些不同.
I have a problem implementing an API that works with Java, but fails to work with cURL. We've gone through everything so far and there must be something that is different between the requests that Java makes and what we make.
在 PHP 中,我们可以通过查看 $_SERVER['HTTP_*']
变量来获取头数据,我们可以从 file_get_contents('php://input'); 获取请求正文;
但是我们无法获得从用户代理发送到客户端的确切数据.
In PHP we can get header data by looking at $_SERVER['HTTP_*']
variables and we can get request body from file_get_contents('php://input');
But we cannot get the exact data sent from user agent to client.
是否可以使用 PHP 获取用户代理发送的完整请求?包括标题和正文?如果是,那怎么办?
Is it possible to get the full request, that user agent sends, with PHP? Headers and body included? If so, then how?
我发现的唯一例子是 here,但是这个以我提到的方式获取正文,而它通过解析 $_SERVER
获取标题,这似乎是一种黑客行为,因为它从来不是 100% 的实际内容发送.
The only example I found is here, but this one gets the body the way I mentioned, while it gets headers by parsing through $_SERVER
, which seems like a hack since it's never 100% of what was actually sent.
感谢所有帮助和提示!
对于标题,您可以尝试 apache_request_headers() 和 body 我不知道除了 file_get_contents('php://input');
for headers you can try apache_request_headers() and for body I dont know other method than file_get_contents('php://input');