简体   繁体   English

为什么我的脚本花这么长时间才能检索标头?

[英]Why is my script taking so long to retrieve headers?

<?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. 我正在研究用于连接到WebDAV,上传文件和断开连接的脚本。 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. 是的,我意识到那里有一个无限的while循环。 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 谢谢

Your problem is because you are sending the Content-Length header with a value of 220 , while not sending any content at all. 您的问题是因为您要发送的Content-Length标头值为220 ,而根本不发送任何内容。 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. 如果连接已关闭, fgets将返回false Send the Connection: close header to tell Apache to end the connection after the data has been sent. 发送Connection: close标头,告诉Apache在发送数据后结束连接。 Your while loop will evaluate to false when the data has been read entirely and the connection has closed, and your loop will exit. 当数据已全部读取且连接已关闭且您的循环将退出时,您的while循环将评估为false

You might want to test it using cURL then. 您可能要使用cURL进行测试。 Try this one out: http://curl.haxx.se/mail/archive-2006-02/0000.html 试试这个: http : //curl.haxx.se/mail/archive-2006-02/0000.html

That way you can see if it's server side or code side. 这样,您可以查看它是服务器端还是代码端。

WebDAV can CHUG if the machine you are connecting to handles lots of traffic in general. 如果您要连接的计算机通常可以处理大量流量,则WebDAV可以进行切换。 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. (如果要连接到生产Web服务器,则可能不是一个选择)。但是,我从未在PHP中处理过此问题。 So the problem certainly could be caused by other reasons. 因此,问题当然可能是由其他原因引起的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM