Best way to hit an API endpoint and catch a response I'm planning on hitting this endpoint from https://postcodes.io
api.postcodes.io/postcodes/[postcode]/validate
and I'm wondering the best way to do that? Is it simply CURL?
Hi @tykus I used Guzzle after your suggestion however it seems that the response I get is generic.. like but that I mean no matter what URL I use, so long as the url is a real one then I get the same response.
I'm not getting the specific responses that I should be getting from that endpoint.
eg
my code:
$client = new \GuzzleHttp\Client();
$vresponse = $client->request('GET', 'https://google.com');
and I get this response
Response {#885 ▼
-reasonPhrase: "OK"
-statusCode: 200
-headers: array:8 [▶]
-headerNames: array:8 [▶]
-protocol: "1.1"
-stream: Stream {#883 ▶}
}
If I use a different URL, for example the actual API endpoint I'm trying to hit, I get the exact same response.
For anyone with the same issue:
$client = new \GuzzleHttp\Client();
$vresponse = $client->get('https://api.postcodes.io/postcodes/'.$postcode.'/validate')->getBody();
$obj = json_decode($vresponse);
dd($obj->result);
The above works
This is what I use (same API) - just simple file_get_contents. No problems so far.
private function postcodeLookup(Speaker $speaker)
{
$result = json_decode(file_get_contents( "https://api.postcodes.io/outcodes/{$speaker->district}/nearest"));
$coordinates['lat'] = round($result->result[0]->latitude,4 );
$coordinates['long'] = round($result->result[0]->longitude,4 );
return $coordinates;
}
Please sign in or create an account to participate in this conversation.