php 中的 HTTP POST 请求速度缓慢

发布于 2024-11-30 09:52:35 字数 495 浏览 2 评论 0原文

我正在尝试将一些数据(JSON 字符串)从 php 脚本发布到 java 服务器(全部由我自己编写)并获取响应。 我尝试了以下代码:

$url="http://localhost:8000/hashmap";
$opts = array('http' => array('method' => 'POST', 'content' => $JSONDATA,'header'=>"Content-Type: application/x-www-form-urlencoded"));
$st = stream_context_create($opts);
echo file_get_contents($url, false,$st);

现在,这段代码实际上可以工作(我得到了正确的答案),但是 file_get_contents 在执行时每次 20 秒都会挂起(我打印了指令前后的时间)。服务器执行的操作是在很短的时间内执行的,我确信一直等待才能得到响应是不正常的。 我错过了什么吗?

I'm trying to POSTing some data (a JSON string) from a php script to a java server (all written by myself) and getting the response back.
I tried the following code:

$url="http://localhost:8000/hashmap";
$opts = array('http' => array('method' => 'POST', 'content' => $JSONDATA,'header'=>"Content-Type: application/x-www-form-urlencoded"));
$st = stream_context_create($opts);
echo file_get_contents($url, false,$st);

Now, this code actually works (I get back as result the right answer), but file_get_contents hangs everytime 20 seconds while being executed (I printed the time before and after the instruction). The operations performed by the server are executed in a small amount of time, and I'm sure it's not normal to wait all this time to get the response.
Am I missing something?

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

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

发布评论

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

评论(2

寒尘 2024-12-07 09:52:35

服务器配置错误,可能无法发送正确的内容大小并使用 HTTP/1.1。

修复服务器或以 HTTP/1.0 请求数据

Badly mis-configured server maybe that doesn't send the right content-size and using HTTP/1.1.

Either fix the server or request the data as HTTP/1.0

指尖上得阳光 2024-12-07 09:52:35

尝试将 Connection: closeContent-Length: strlen($JSONDATA) 标头添加到 $opts 中。

另外,如果您想避免使用扩展程序,请查看这个class 我前段时间编写的,仅使用 PHP 核心执行 HTTP 请求。它适用于 PHP4(这就是我编写它的原因)和 PHP5,它需要的唯一扩展是 OpenSSL,只有当您想要执行 HTTPS 请求时才需要它。记录在顶部的评论中。

支持各种内容 - GET、POST、PUT 等,包括文件上传、cookie、自动重定向处理。我在一个经常使用 PHP/4.3.10 的平台上经常使用它,而且它工作得很好......即使我自己这么说......

Try adding Connection: close and Content-Length: strlen($JSONDATA) headers to the $opts.

Also, if you want to avoid using extensions, have a look at this class I wrote some time ago to perform HTTP requests using PHP core only. It works on PHP4 (which is why I wrote it) and PHP5, and the only extension it ever requires is OpenSSL, and you only need that if you want to do an HTTPS request. Documented(ish) in comments at the top.

Supports all sorts of stuff - GET, POST, PUT and more, including file uploads, cookies, automatic redirect handling. I have used it quite a lot on a platform I work with regularly that is stuck with PHP/4.3.10 and it works beautifully... Even if I do say so myself...

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