为什么我的脚本需要很长时间才能检索标头?

发布于 2024-10-02 18:30:12 字数 1652 浏览 6 评论 0原文

<?php
set_time_limit(0);

$errorArr = array();
if (!isset($argv[1]))
{
    array_push($errorArr, "You forgot to enter a host.");
}
if ((isset($argv[1])) AND (!filter_var($argv[1], FILTER_VALIDATE_IP)))
{
    array_push($errorArr, "The host you entered is not a valid IP address.");
}
if (!isset($argv[2]))
{
    array_push($errorArr, "You forgot to select a port.");
}
if (!empty($errorArr))
{
    echo "You have the following errors:\n";
    print_r($errorArr);
    die("Syntax is as follows: php {$argv[0]} host port\n");
}

$host = $argv[1];
$port = $argv[2];

echo ":::Connecting...\n";
$fh = fsockopen($host, $port);
if (!$fh)
{
    die(":::Connection failed.\n:::Aborting.\n");
}
echo ":::Connected!\n:::Sending headers.\n";

$header = "PROPFIND /webdav/ HTTP/1.1\r\n";
$header .= "Host: {$host}\r\n";
$header .= "User-Agent: BitKinex/3.2.3\r\n";
$header .= "Accept: */*\r\n";
$header .= "Pragma: no-cache\r\n";
$header .= "Cache-Control: no-cache\r\n";
$header .= "Depth: 1\r\n";
$header .= "Content-Length: 220\r\n";
$header .= "Content-Type: text/xml\r\n\r\n\r\n";
if (!fwrite($fh, $header))
{
    die(":::Couldn't send headers. Aborting.\n");
}
$exHeader = explode("\r\n", $header);
foreach ($exHeader as $ecHeader)
{
    echo "<<<{$ecHeader}\n";
}
echo "\n:::Retrieving syntax...\n";
while(1)
{
    while ($data = fgets($fh, 512))
    {
        echo ">>>{$data}";
        flush();
    }
}
?>

我正在编写一个脚本来连接到 WebDAV、上传文件和断开连接。它可以很好地连接和发送标头,但需要很长时间才能检索语法。有时,需要几分钟,我不明白为什么。是我的代码有问题吗?

是的,我意识到那里有一个无限的 while 循环。这是故意这样做的,因为我还没有弄清楚如何知道服务器何时完成向我发送信息。所以我想这是另一个问题,如果有人可以对此提供见解。

谢谢

<?php
set_time_limit(0);

$errorArr = array();
if (!isset($argv[1]))
{
    array_push($errorArr, "You forgot to enter a host.");
}
if ((isset($argv[1])) AND (!filter_var($argv[1], FILTER_VALIDATE_IP)))
{
    array_push($errorArr, "The host you entered is not a valid IP address.");
}
if (!isset($argv[2]))
{
    array_push($errorArr, "You forgot to select a port.");
}
if (!empty($errorArr))
{
    echo "You have the following errors:\n";
    print_r($errorArr);
    die("Syntax is as follows: php {$argv[0]} host port\n");
}

$host = $argv[1];
$port = $argv[2];

echo ":::Connecting...\n";
$fh = fsockopen($host, $port);
if (!$fh)
{
    die(":::Connection failed.\n:::Aborting.\n");
}
echo ":::Connected!\n:::Sending headers.\n";

$header = "PROPFIND /webdav/ HTTP/1.1\r\n";
$header .= "Host: {$host}\r\n";
$header .= "User-Agent: BitKinex/3.2.3\r\n";
$header .= "Accept: */*\r\n";
$header .= "Pragma: no-cache\r\n";
$header .= "Cache-Control: no-cache\r\n";
$header .= "Depth: 1\r\n";
$header .= "Content-Length: 220\r\n";
$header .= "Content-Type: text/xml\r\n\r\n\r\n";
if (!fwrite($fh, $header))
{
    die(":::Couldn't send headers. Aborting.\n");
}
$exHeader = explode("\r\n", $header);
foreach ($exHeader as $ecHeader)
{
    echo "<<<{$ecHeader}\n";
}
echo "\n:::Retrieving syntax...\n";
while(1)
{
    while ($data = fgets($fh, 512))
    {
        echo ">>>{$data}";
        flush();
    }
}
?>

I'm working on a script to connect to WebDAV, upload a file, and disconnect. It connects and sends headers fine, but then it takes forever to retrieve syntax. At times, it takes several minutes, and I can't understand why. Is it a problem in my code?

And yes, I realize there's an infinite while loop there. That's done on purpose, because I haven't figured out how to know when the server is done sending information to me. So I guess that's another question, if anyone could provide insight to that.

Thanks

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

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

发布评论

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

评论(3

独﹏钓一江月 2024-10-09 18:30:12

您的问题是因为您发送的 Content-Length 标头的值为 220,但根本不发送任何内容。服务器挂在那里等待内容,但它永远不会到达......

对于无限循环的事情,你根本不需要它。如果连接已关闭,fgets 将返回 false。发送 Connection: close 标头告诉 Apache 在发送数据后结束连接。当数据已完全读取并且连接已关闭时,您的 while 循环将评估为 false,并且您的循环将退出。

Your problem is because you are sending the Content-Length header with a value of 220, while not sending any content at all. The server hangs in there expecting content, but it never arrives...

And for your infinite loop thing, you don't need it at all. fgets will return false if the connection has closed. Send the Connection: close header to tell Apache to end the connection after the data has been sent. Your while loop will evaluate to false when the data has been read entirely and the connection has closed, and your loop will exit.

我要还你自由 2024-10-09 18:30:12

然后您可能想使用 cURL 来测试它。试试这个: http://curl.haxx.se/mail/ archive-2006-02/0000.html

这样你就可以看到它是服务器端还是代码端。

You might want to test it using cURL then. Try this one out: http://curl.haxx.se/mail/archive-2006-02/0000.html

That way you can see if it's server side or code side.

绳情 2024-10-09 18:30:12

如果您连接的计算机通常处理大量流量,则 WebDAV 可能会突然出现异常。尤其是大量的网络流量。原因很复杂,但我过去使用的解决方案主要涉及围绕延迟进行编码。要么将内容转储到队列中等待,要么将内容推送到负载不重但更直接连接到相关服务器的盒子,并且可以通过不同方式将文件推送到该盒子。

然而,这一切都需要访问权限,如果您可以控制要连接的计算机,您应该能够重新配置它们以给予自己优先权。 (如果您连接到生产 Web 服务器,这可能不是一个选项)但是,我从来没有在 PHP 中处理过这个问题。所以这个问题肯定可能是由其他原因引起的。

WebDAV can CHUG if the machine you are connecting to handles lots of traffic in general. And especially lots of web traffic. The reasons are complex, but solutions I have used in the past have primarily involved coding around the delay. Either by dumping things in a line to wait, or by pushing things to a box that isn't under heavy load but is more directly connected to the server in question and can push the files to it via different means.

This all requires access, however, and if you have control over the machines you are connecting to, you should be able to reconfigure them to give yourself priority. (which may not be an option if you are connecting to a production web server) However, I've never had to deal with this in PHP. So the problem certainly could be caused by other reasons.

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