Recursion on Request

199 views Asked by At

I am developing some Request with Symfony to get an SSL Report from SSLLabs.

Problem: If I send this Request, I get a Response and check one Parameter. There are three Options for this Parameter ("ERROR", "IN_PROGRESS", "READY") which I have to handle.

public function getSSLReport(string $request)
{
    try{
        $result = null;
        $httpClient = new \GuzzleHttp\Client();
        $response = $httpClient->request('GET', 'https://api.ssllabs.com/api/v3/analyze?host='.$request.'&all=on');
        $result = json_decode($response->getBody()->getContents(), true);
        if($result['status'] == "READY") {
            return new JsonResponse($result);
        } else if($result['status'] == "ERROR") {
            return new JsonResponse($result['statusMessage']);
        } else {
            $this->getSSLReport($request);
        }
    }catch (\Exception $ex)
    {
        return new JsonResponse($ex);
    }
}

I am using some Recursion to call this Method again if the $result is IN_PROGRESS. But the Request is Pending all the Time, and canceled after 30 seconds.

If I get some Response where "status" == "READY" it works fine.

1

There are 1 answers

1
Stephan Vierkant On

Add sleep to wait for a couple of seconds:

    if($result['status'] === "READY") {
        return new JsonResponse($result);
    }

    if($result['status'] === "ERROR") {
        return new JsonResponse($result['statusMessage']);
    }

    if($result['status'] === "IN_PROGRESS") {
        sleep(5);
        return $this->getSSLReport($request);
    }

    throw new \Exception('Unknown status from SSLLabs');

What I've changed:

  • removed the else and replaced elseif by if.
  • Replaced == by ====.
  • Throw an exception when the status from the API is unknown by your code. Instead of retrying, the script should exit in case of an unknown status.