【问题标题】:Laravel Guzzle Return errorLaravel Guzzle 返回错误
【发布时间】:2018-11-26 11:15:07
【问题描述】:

我正在使用laravel 5.5.*guzzlehttp/guzzle ^6.3。我在同一个项目 (using laravel api.php) 中创建了 API,并在同一个项目 (using laravel web.php) 上使用 API,并给出了 throttle 120 per second

一切正常,但在使用 guzzle 解析时突然出现以下错误

{
    "error": "FatalErrorException",
    "reason": "Allowed memory size of 536870912 bytes exhausted (tried to allocate 266342400 bytes)",
    "code": 1,
    "trace": []
}

使用XAMPP servermemory_limit=2048M。 如果我在浏览器中访问 API,它会加载正常

Guzzle 解析代码如下

$client = new Client([
    'base_uri' => env('API_URL'),
    'headers' => ['Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8'],
    'debug' => true,
]);

请有人帮我解决这个问题?即使我清除了缓存也生成了一个新密钥

【问题讨论】:

    标签: php laravel laravel-5.5 guzzle guzzle6


    【解决方案1】:

    您的脚本内存泄漏总是由于在 while loop 中重新定义一个 guzzle 造成的,请尝试使用 memory_get_usage 来调试您的脚本的内存使用情况。

    并且不要在循环中定义类,在您必须在循环之外使用适配器的情况下,如下所示:

    $adapter = new \GuzzleHttp\Adapter\Curl\MultiAdapter(
        new \GuzzleHttp\Message\MessageFactory()
    );
    
    while (true) {
        $client = new GuzzleHttp\Client(['adapter' => $adapter]);
        $client->get('http://example.com/guzzle-server');
        echo memory_get_usage() . "\n";
        usleep(20000);
    }
    

    编辑
    您可以在内存吞噬者上进行更多调试:

    Memprof 是一个 php 扩展,可以帮助找到那些吃内存的 sn-ps,特别是在面向对象的代码中。 [SOURCE]

    EDIT2 :
    作为新的 cmets ,在调用您的 api 之前和之后尝试通过 memory_get_usage 函数查看内存使用情况。

    【讨论】:

    • 我已经添加了 $client = new Client([ 'base_uri' => $url, 'headers' => ['Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8'], /* 'headers' => ['Accept' => 'application/json', 'Content-Type' => 'application/json',]*/ 'debug' => true, ]);性状
    • 即使我只检索了一条记录。它会影响吗?
    • 它可能不是一次调用,您也可以调试它,查看编辑答案
    • 谢谢,但对我来说一次调用 api 也给出了同样的错误,但昨天它工作正常,即使我重新启动了 apache 并且我的系统没有用
    • 在调用 api 之前和之后查看 memory_get_usage,以检查问题是否真的来自 api 调用?
    猜你喜欢
    • 2018-06-01
    • 2015-06-25
    • 1970-01-01
    • 2020-10-15
    • 1970-01-01
    • 2023-03-04
    • 2014-10-31
    • 1970-01-01
    • 2017-09-15
    相关资源
    最近更新 更多