I’m currently trying to fetch data from an external source (not a Wp site) to a Wp site using the wp_remote_get method.
The site that i want to get data from doesn’t have an actual json file to make the call instead they provide this link to make the request (https://fcc-weather-api.glitch.me/api/current?lat=35&lon=139.
I’m using the below code to make the call.
Is there a way to parse the response in php with html elements so i can properly display it on my website?
Any help would be appreciated.
$url = ' https://fcc-weather-api.glitch.me/api/current?lat=35&lon=139';
$request = wp_remote_get( $url );
if(is_wp_error($request)) {
return false;
} else {
$body = $request['body'];
}
echo $body;
$api_response = json_decode( wp_remote_retrieve_body( $response ), true );
The response
{ "coord":{ "lon":159, "lat":35 }, "weather":[ { "id":500, "main":"Rain", "description":"light rain", "icon":"https://cdn.glitch.com/6e8889e5-7a72-48f0-a061-863548450de5%2F10n.png?1499366021399" } ], "base":"stations", "main":{ "temp":22.59, "pressure":1027.45, "humidity":100, "temp_min":22.59, "temp_max":22.59, "sea_level":1027.47, "grnd_level":1027.45 }, "wind":{ "speed":8.12, "deg":246.503 }, "rain":{ "3h":0.45 }, "clouds":{ "all":92 }, "dt":1499521932, "sys":{ "message":0.0034, "sunrise":1499451436, "sunset":1499503246 }, "id":0, "name":"", "cod":200 }
2
Answers
Does this help? You had
$response
in there – which was never defined.You can use your
$api response
to display data. check below code. I did not display all data but can be displayed it by the printecho "<pre>"; print_r($api_response); echo "</pre>";
The above code displayed something like this. you can modify based on your requirements.