How to close the request after its complete using Guzzle 6.x Client

Viewed 6037

I have a task to identify the existence of the links. As a result I am using guzzle client to identify if the link is existence or not, i.e if the response header received is 200, then the link is existence else not.

Below is my code snippet

public function checkUrl($url) {
    $result['isValid'] = false;
    try {
        $response = $this->client->get($url, ['verify', false]);
    } catch (\Exception $ex) {
        $result['isValid'] = false;
        $result['message'] = 'Some error message';
        return $result;
    }
    if ($response->getStatusCode() == Response::STATUS_CODE_200) {
        $result['isValid'] = true;
    }
    $result['message'] = 'Success - ' . $response->getStatusCode();
    $response->getBody()->close();

    return $result;
}

where $this->client is initialized to GuzzleHttp\Client object once in the constructor

When I run my script, after some time it throws me the error as follows: PHP Fatal error: Uncaught ErrorException: include(/project/vendor/zendframework/zend-view/src/Model/ConsoleModel.php): failed to open stream: Too many open files

And when I check the list of open files using the command lsof -p <process id> -n, I noticed that there lots of open files are as a result of guzzle request (i.e curl responses) and it seems to be the cause of this exception.

Is there any suggestion for the solution through which I can close those responses?

0 Answers
Related