5

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?

Krish Damani
  • 185
  • 2
  • 14
  • I wonder if theres a bug in Guzzle not closing those Curl requests. – Flosculus May 26 '17 at 14:25
  • @Flosculus What should be the best way to confirm whether its Guzzle bug or not? – Krish Damani May 26 '17 at 14:35
  • Fire up XDebug, and dive into Guzzle. We can probably help though, if you know exactly what version you are using. – Flosculus May 26 '17 at 14:36
  • @Flosculus I am using 6.2.3 version – Krish Damani May 26 '17 at 14:38
  • 2
    Guzzle does not close the open streams by as it does not know for how long you need them. You can either manually close the open streams by calling `$response->getBody()->close()` or you can let the Stream object's destructor do it (for which you obviously need to "destruct" that object). – mark.sagikazar May 26 '17 at 14:41
  • @mark.sagikazar, I tried `$response->getBody()->close()` but I still see there are open curl responses and I still get the same error for `Too many files open`. – Krish Damani May 29 '17 at 06:13
  • @KD-Freelancer did you solve the issue? – tylik Aug 24 '18 at 09:04
  • @tylik No could not reach to any proper solution, hence have changed the implementation itself. – Krish Damani Sep 25 '18 at 07:11
  • @KD-Freelancer could you please describe in couple of words? I can't find any proper solution. Have you dropped using Guzzlehttp, or somehow made it work without this bug? – tylik Sep 26 '18 at 15:17
  • Related issue. #1927 https://github.com/guzzle/guzzle/issues/1927 – Liam Mitchell Dec 07 '18 at 14:52

0 Answers0