1 year ago

#368331

test-img

Ilya M.

guzzle: TooManyRedirectsException after downgrading

The code worked correctly on version 7+, however, when the version is downgraded to 6, the function falls with an error.

Code:

<?
define(ZIP_TO_LOCATION_APP, "https://tools.usps.com/tools/app/ziplookup/cityByZip");

function get_location_from_zip($zip_code) {
  $client = new GuzzleHttp\Client();
  $response = $client->request("POST", ZIP_TO_LOCATION_APP, [
    'form_params' => [
        'zip' => $zip_code
    ]
  ]);
  if ($response->getStatusCode() == 200) {
    $contents = $response->getBody()->getContents();
    $contents = json_decode($contents, true);
    $contents["error"] = false;
    return $contents;
  } else {
    return array(
      "error" => true
    );
  }
}

Output:

Fatal error: Uncaught GuzzleHttp\Exception\TooManyRedirectsException: Will not follow more than 5 redirects in C:\OpenServer\domains\job-board.loc\api\classes\composer\vendor\guzzlehttp\guzzle\src\RedirectMiddleware.php:159 Stack trace: #0 C:\OpenServer\domains\job-board.loc\api\classes\composer\vendor\guzzlehttp\guzzle\src\RedirectMiddleware.php(94): GuzzleHttp\RedirectMiddleware->guardMax(Object(GuzzleHttp\Psr7\Request), Array) #1 C:\OpenServer\domains\job-board.loc\api\classes\composer\vendor\guzzlehttp\guzzle\src\RedirectMiddleware.php(72): GuzzleHttp\RedirectMiddleware->checkRedirect(Object(GuzzleHttp\Psr7\Request), Array, Object(GuzzleHttp\Psr7\Response)) #2 C:\OpenServer\domains\job-board.loc\api\classes\composer\vendor\guzzlehttp\promises\src\FulfilledPromise.php(41): GuzzleHttp\RedirectMiddleware->GuzzleHttp\{closure}(Object(GuzzleHttp\Psr7\Response)) #3 C:\OpenServer\domains\job-board.loc\api\classes\composer\vendor\guzzlehttp\promises\src\TaskQueue.php(48): GuzzleHttp\Promise\FulfilledPromise::GuzzleHttp\Promis in C:\OpenServer\domains\job-board.loc\api\classes\composer\vendor\guzzlehttp\guzzle\src\RedirectMiddleware.php on line 159

I tried playing around with the allow_redirects option, this can hide the exception, but it still doesn't return a response.

php

post

redirect

http-post

guzzle

0 Answers

Your Answer

Accepted video resources