Making 2 API GET requests
Hey guys how can I make 2 requests for 2 different end points on a URL that comes from an API?
public $search;
public $searchResults = [];
public function updatedSearch($newValue)
{
if(strlen($this->search) < 3) {
$this->searchResults = [];
return;
}
$response = Http::get('http://thvid-api.herokuapp.com/videos/game/'.$this->search)->json();
$this->searchResults = $response;
}
This works fine but I wish to make another query in the same component but to another URL. Is that possible? Does a method exists for this? Thanks.
@warpig it should be possible:
public $search;
public $searchResults = [];
public function updatedSearch($newValue)
{
if(strlen($this->search) < 3) {
$this->searchResults = [];
return;
}
$response1 = Http::get('http://thvid-api.herokuapp.com/videos/game/'.$this->search)->json();
$response2 = Http::get('http://site.com/'.$this->search)->json();
$this->searchResults = array_merge($response1, $response2);
}
For array_merge, you might need to json_decode the responses before array_merge then json_encodeagain.
Apply json_decode to both responses?
@warpig yes but you need to test it because you translated your responses to JSON using ->json().
I see, it worked just fine with the json() thanks!
Please or to participate in this conversation.