B
B
BonBon Slick2021-07-05 00:59:43
PHP
BonBon Slick, 2021-07-05 00:59:43

Time limit for CurlHttpClient / curl or Guzzle?

It is necessary to implement chain calls to apishek in the case when one fell off for unknown reasons.

Previously use free api for your tools

use Symfony\Component\HttpClient\CurlHttpClient;

//        https://api.ip.sb/geoip/***  --- было
//        http://ip-api.com/php/*** --- стало, старый роут 522 более недоступен
        $data = (new CurlHttpClient())
            ->request(Request::METHOD_GET, sprintf(' http://ip-api.com/php/%s', $ip))
            ->toArray()
        ;

So, you need to have 2-3 more services in stock, and set a time limit on external API requests for geolocation.

What is the best way to implement this and why?
What free service do you recommend?
Minimum 25 requests per minute.
Unfortunately, such BC problems can occur too often, and if you use unreliable APIs without insurance in production, you can lose users.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BonBon Slick, 2021-07-07
@BonBonSlick

https://everything.curl.dev/usingcurl/timeouts#:~:... .

...
 $data = [];
        foreach ($services as $service) {
            try {
                $data = (new CurlHttpClient())
            ->request(Request::METHOD_GET, sprintf('****%s', $ip), ['timeout' => 0.01])
            ->toArray()
        ;
                if (true === isset($data['city'])) {
                    break;
                }
            } catch (\Exception $exception) {
                $this->logError(__METHOD__, ['message' => $exception->getMessage()]);
            }
        }
...
        return new IPInfoDTO(
            $long,
            $lat,
            $ccAlpha2,
            $postalCode
        );

This is a rough draft as it is a consumable, temporary service for now. Ideally, instead of links, services should be called in chain responsibility, calls pattern on actions. Expression at the timeout that we set, 0.01 sec for tests.
https://stackoverflow.com/questions/2582057/settin...
https://docs.guzzlephp.org/en/stable/request-optio...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question