在没有看到脚本的情况下,很难给您一个明确的答案,但是您需要做的是确保您的脚本正在适当地等待数据。你绝对不应该做的是调用stream_set_timeout($fp, 0);
或stream_set_blocking($fp, 0);
在你的文件指针上。
做这样的事情应该避免赛车的脚本的基本结构是这样的:
// Open the file pointer and set blocking mode
$fp = fopen('http://www.domain.tld/somepage.file','r');
stream_set_timeout($fp, 1);
stream_set_blocking($fp, 1);
while (!feof($fp)) { // This should loop until the server closes the connection
// This line should be pretty much the first line in the loop
// It will try and fetch a line from $fp, and block for 1 second
// or until one is available. This should help avoid racing
// You can also use fread() in the same way if necessary
if (($str = fgets($fp)) === FALSE) continue;
// rest of app logic goes here
}
您也可以使用sleep()
/usleep()
来避免赛车,但更好的方法是依靠阻塞函数调用来进行阻塞。如果它适用于一个操作系统但不能在另一个操作系统上运行,请尝试明确设置阻塞模式/行为,如上所述。
如果您无法通过调用fopen()
传递 HTTP URL 来实现此功能,则可能是 PHP 中的 HTTP 包装器实现存在问题。要解决此问题,您可以fsockopen()
自己使用和处理请求。这并不太难,特别是如果您只需要发送一个请求并读取一个恒定的流响应。