E
E
Eugene2017-09-26 03:04:37
PHP
Eugene, 2017-09-26 03:04:37

How to make two consecutive requests with Guzzle, where the second uses the result of the first?

Hello,
Can you please tell me how to implement two consecutive requests using Guzzle, where the second one uses the result of the first one?
There is a test page with a token and API scumtron.ru/api/?auth=, which generates HTTP 501-504 errors with a 10% randomness.
It is necessary to get a token (each time unique) with the first request using preg_match and use it in the second request as an auth parameter. Ideally, you want to catch HTTP errors like 501-504 and, in case of similar ones, make an attempt to reparse several times, as well as write all unsuccessful requests to the log file.
At this stage, it turns out to parse the token, but I can’t find examples of how to use the received data for the second request in one connection. Help me please.

use GuzzleHttp\Client;

$client = new Client([
  'base_uri' => 'http://scumtron.ru',
  'timeout'  => 3,
  'connect_timeout' => 3,
  'allow_redirects' => false
]);

$response = $client->request('GET', '/');
$body = $response->getBody();
preg_match("#<b>([0-9]{8})</b>#", $body, $token);
echo $token[1];

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AlikDex, 2017-09-26
@Scumtron

Consistently you do the second request if there is a result of the first. It's obvious.
In addition, Guzzle can handle request errors, including those with 500+ response codes.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question