php - 如何限制 GET/PHP5 中每秒与服务器的连接数?

标签 php api get

下面的脚本会获取另一种语言的单词并与服务器建立连接。

But, there are so many separate string entities that some of them return as empty values. StackOverflow fellow @Pekka correctly assessed this to the limitation of Google: Timing Out the result.

Q1.如何使连接更加牢固/可靠,尽管会牺牲速度?
第二季度。如何有意限制每秒与服务器建立的连接数?

只要返回的值是正确的,我愿意牺牲速度,即使这会导致120秒的延迟。现在一切都在 0.5 秒左右开始和完成,翻译中存在各种间隙。几乎就像荷兰奶酪(有孔),我想要没有孔的奶酪,即使这意味着更长的等待时间。

正如你所看到的,我自己的让脚本休眠 1/4 秒的解决方案不能称为优雅......如何从这里继续?

    $url='http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=' . rawurlencode($string) . '&langpair=' . rawurlencode($from.'|'.$to);
    $response   = file_get_contents($url,
            null,
            stream_context_create(
            array(
            'http'=>array(
            'method'=>"GET",
            'header'=>"Referer: http://test.com/\r\n"
            )
            )
        ));
usleep(250000); # means 1/4 of second deliberate pauze
return self::cleanText($response);
}

最佳答案

How can I deliberatly limit the amount of connections made per second to the server?

这要看情况。在理想的世界中,如果您期望任何级别的流量,您可能希望您的抓取工具成为您通过 message or work queue 进行通信的守护进程。 。在这种情况下,守护进程将能够严格控制每秒的请求并适本地限制事物。

听起来您实际上是根据用户请求实时进行此操作。说实话,你目前的 sleep 策略还不错。当然,它“粗糙”,但它很简单而且有效。当您可能有多个用户同时发出请求时,就会出现麻烦,在这种情况下,这两个请求将忽略另一个请求,并且最终您每秒的请求数将超过服务允许的数量。

这里有一些策略。如果 URL 永远不会改变,也就是说,您只限制单个服务,那么您基本上需要 semaphore协调多个脚本。

考虑使用简单的锁定文件。或者,更准确地说,锁定文件上的文件锁:

// Open our lock file for reading and writing; 
// create it if it doesn't exist, 
// don't truncate, 
// don't relocate the file pointer.
$fh = fopen('./lock.file', 'c+');
foreach($list_of_requests as $request_or_whatever) {
// At the top of the loop, establish the lock.
    $ok = flock($fh, LOCK_EX):
    if(!$ok) {
        echo "Wow, the lock failed, that shouldn't ever happen.";
        break; // Exit the loop.
    }
// Insert the actual request *and* sleep code here.
    $foo->getTranslation(...);
// Once the request is made and we've slept, release the lock
// to allow another process that might be waiting for the lock
// to grab it and run.
   flock($fh, LOCK_UN);
}
fclose($fh);

这在大多数情况下效果很好。如果您使用超低成本或低质量的共享主机,由于底层文件系统(不)工作,锁可能会适得其反。 flock is also a bit finicky on Windows .

如果您要处理多个服务,事情会变得更加棘手。我的第一 react 是在数据库中创建一个表,并开始跟踪发出的每个请求,如果在过去 Z 秒内向域 Y 发出了超过 X 个请求,则添加额外的限制。

Q1.How can I make the connection more strong/reliable, albeit at the cost of speed?

如果您坚持使用 Google 翻译,您可能需要 switch to the Translate v2 RESTful API 。这需要 API key ,但注册过程将迫使您阅读他们的 TOS,其中应记录他们的请求/期限最大限制。由此,您可以使系统限制请求到其服务支持和维护可靠性的任何速率。

关于php - 如何限制 GET/PHP5 中每秒与服务器的连接数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5446824/

相关文章:

php - 如何在我的 View 页面中使用打印输出扩展来打印 View 页面?

Android - 缓慢的数据检索

ios - 可以通过 "Postman software"获取 XML 数据,但无法通过 SWIFT 获取数据

api - 使用 Python 的 Spotify API 授权代码流

php - mysql 日期差异 :get data between days

php - 用于检查脚本性能的 PHP 函数列表

php - 在 PHP 中将 utf8 转换为 latin1。所有大于 255 的字符都转换为 char 引用

javascript - 使用 Angular http 从 firebase 实时数据库获取数据

get - Perforce获取最新修订版不获取 check out 文件吗?

java - 如何为类中的参数创建 get 和 set 方法?