【问题标题】:PHP: large amount of requests with file_get_contentsPHP:使用 file_get_contents 的大量请求
【发布时间】:2015-02-26 10:49:24
【问题描述】:

我目前有一个数据库表,其中包含组织编号 [10.02 亿行]。

现在,我想做的是从远程网站 API 获取组织的电话号码。我已经完成了这项工作,但是在向 API 发出 30 到 50 次左右的请求后,我没有看到表格有任何新的变化,我将电话号码插入其中。我还有 100 万行以上的行可以从中获取电话号码,但我似乎只能获取少量行。

提前感谢您的帮助。

我不知道这是否会有所帮助,但这是我用来执行此操作的代码。

// Remove timeout limit
// This is going to take alot of time!
set_time_limit(0); 

// Initialize...
include $_SERVER['DOCUMENT_ROOT'] . '/core/Init.php';

// Profiles
$url = 'http://finnrett.no/API/business/get/quickresults?q=';
$profile_url = 'http://finnrett.no/API/business/get/profile?id=';

// Select names
$sql = 'SELECT organisasjonsnummer FROM brreg  ORDER BY id LIMIT 10000';
$result = $Dbh -> query($sql, []);

// Each name
foreach ($result as $orgnr) {

    // Pre for output explananation
    echo'<pre>';

    // Grab json from quick results url
    $bedrift = json_decode(file_get_contents($url.$orgnr['organisasjonsnummer']));
    $ID = get_object_vars($bedrift[0])['ID'];

    $profile = json_decode(file_get_contents($profile_url.$ID));
    $CONTACT = get_object_vars($profile);
    $number = $CONTACT['contact'] ? $CONTACT['contact']: '0';

    $sql = 'INSERT INTO profiles (orgnr, telefon) VALUES (:orgnr, :telefon)';
    $args = ['orgnr' => $orgnr['organisasjonsnummer'], 'telefon' => $number];
    if (!$Dbh -> query($sql, $args)) {
        echo 'Sjekk opp org: ' . $orgnr['organisasjonsnummer'] . ' fordi her skjedde det noe galt.';
    }


}

看起来我通过选择 curl 而不是 file_get_contents 解决了这个问题。

【问题讨论】:

  • 只是延迟问题?您可以获取所有记录,但它只是在请求 30~50 条记录后才开始响应?
  • 没有。在 30-50 条记录之后,它不会继续获取任何内容。我得到了 30-50 的数字,但在那之后,什么都没有。
  • 您使用的是什么 API?只是一个疯狂的猜测,但也许它是一种避免 DoS 的机制,无论如何你可以参考 API 文档 os call support。
  • finnrett.no/API/business/get/profile?id=## finnrett.no/API/business/get/quickresults?q=## Theese API。它不是关于它们的文档。但你可能是对的。这可能是某种保护。
  • @Kaizokupuffball 不要用你的解决方案编辑你的问题,而是回答它;)

标签: php mysql api file-get-contents


【解决方案1】:

通过切换到curl而不是file_get_contents()解决了这个问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-13
    • 2011-11-13
    • 2020-07-05
    • 2013-10-10
    • 2017-08-14
    • 1970-01-01
    • 2016-06-23
    相关资源
    最近更新 更多