Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

lukaszj's avatar

Two api request

Hello, I have this test code, API queries. A single call for countries and currencies returns results correctly. However, when there are two calls (countries and currencies together) no results (the browser is still processing). Please help

Route::get('testapi', function () {

$countries = Countries::getApiCountries(); // calling just that. it works

$currencies = Currency::getApiCurrencies(); // calling just that. it works

//but calling $countries and $currencies doesn't work :(

return view('cards.create', compact('countries', 'currencies')); });

0 likes
17 replies
jamalroger's avatar

@lukaszj you should return resulta from controller if want to do a api

Route::get('testapi', function () {

$countries = Countries::getApiCountries(); // calling just that. it works

$currencies = Currency::getApiCurrencies() //



return ['countries'=>$countries,'currencies '=>$currencies ];
	
} 

and also should be called in api.php file because it's api

ahmeddabak's avatar

Please share the logic for Countries::getApiCountries()

lukaszj's avatar

this is downloading data from an external api. If I comment on currencies returns countries correct and vice versa.

lukaszj's avatar

public static function getApiCountries() { $client = new Api(config('api.url')); $responseCountries = $client->request( 'countries', Request::METHOD_GET, [] );

    return $responseCountries;
}

And Api.php

public function __construct(string $baseUrl,array $options = []) { $this->client = new Client();

    $this->baseUrl = trim($baseUrl, '/');
    $this->options = array_merge([
        'headers' => [
            'Accept' => 'application/json',
            'Content-type' => 'application/json'
        ]
    ], $options);

}

public function request(string $endpoint, string $method, array $data = [])
{

    $data = $data ? ['query' => $data] : [];

    $options = array_merge($this->options, $data);

    $endpoint = sprintf('%s/%s', $this->baseUrl, trim($endpoint, '/'));

    return json_decode($this->client->request($method, $endpoint, $options)->getBody()->getContents(), true);
}
jamalroger's avatar

@lukaszj pleasee call dd($countries) before you passing data to view to show the content of variable

lukaszj's avatar

array:1 [▼ "data" => array:23 [▼ 0 => array:5 [▶] 1 => array:5 [▶] 2 => array:5 [▶] 3 => array:5 [▶] 4 => array:5 [▶] 5 => array:5 [▶] 6 => array:5 [▶] 7 => array:5 [▶] 8 => array:5 [▶] 9 => array:5 [▶] 10 => array:5 [▶] 11 => array:5 [▶] 12 => array:5 [▶] 13 => array:5 [▶] 14 => array:5 [▶] 15 => array:5 [▶] 16 => array:5 [▶] 17 => array:5 [▶] 18 => array:5 [▶] 19 => array:5 [▶] 20 => array:5 [▶] 21 => array:5 [▶] 22 => array:5 [▶] ] ]

code: Route::get('testapi', function () {

$countries = Countries::getApiCountries();

// $currencies = Currency::getApiCurrencies();


dd($countries);

});

It works, but when I uncomment the $currency, it won't work.

jamalroger's avatar

@lukaszj

now just pass data the to view like that

return view('cards.create', ['countries'=>$countries,'currencies '=>$currencies ]);

and in the View

@foreach($countries['data'] as $c )
code here
@endforeach
lukaszj's avatar

JAMALROGER that works, but i need $countries and $currencies both.

=========== IT WORKS====================

$countries = Countries::getApiCountries();

// $currencies = Currency::getApiCurrencies();

=========== IT WORKS====================

//$countries = Countries::getApiCountries();

$currencies = Currency::getApiCurrencies();

=========== NOT WORKS====================

$countries = Countries::getApiCountries();

$currencies = Currency::getApiCurrencies();

I need last option, because in my view I have two select countries and currency. I need two response from outside server api

Snapey's avatar
Snapey
Best Answer
Level 122

you using php artisan serve ?

lukaszj's avatar

no. I use nginx as local server, but I do not use php artisan serve.

ahmeddabak's avatar

What get returned when you call both, do you get null or an error

lukaszj's avatar

However, when there are two calls (countries and currencies both) no results (the browser is still processing). The browser wheel is spinning :)

ahmeddabak's avatar

Since both apis works correctly when you call them separately, and you do not get an error but the browser keep spinning .

Try setting a timeout on the http client you are using in the Api class prevent the call from taking too long. but this won't solve your problem, maybe the api you are trying to call has a rate limit and only allows one call per minute or maybe the called data is too big that it takes a long time to download, try contacting their support.

martinbean's avatar

@lukaszj Do countries and currencies change that often you need to get them from an API…?

It might help if you said a) what API you’re using to get the countries and currencies, and b) what you mean by “it doesn’t work”. Saying it doesn’t work isn’t very helpful. Why doesn’t it work? Do you get an error message…? Where do the Countries and Currency classes you’re using come from?

blade84's avatar

It seems like u are limited by external API (multiple requests at the same time). Just for test can u try something like this? :

$countries = Countries::getApiCountries(); 
sleep(1);
$currencies = Currency::getApiCurrencies(); 
lukaszj's avatar

Thanks all for reply. I'm using php artisan serve command, it's work now. Thx Snapey :)

Please or to participate in this conversation.