Requesitng Weather details from API

Hi All

Looking for some help on this please, any help would be greatly appreciated.

I have a form with fields like: current temp, current cloud cover and humidity.
So we can record the current weather variables at the time the data is captured.

I would like to use a weather api to retrieve that data and auto populate those fields.

The api I have chosen is Weatherstack and the url looks like this:

http://api.weatherstack.com/current
? access_key = YOUR_ACCESS_KEY
& query = New York

My thinking is to create a php method and then an ajax event to trigger that php method and update the fields with the data retrieved from the weatherstack api.
I need assistance on the php method code to integrate the weatherstack url please.

I assume it would use a GET?

Have you tried the sc_webservice Macro?

i figured it out, for anyone looking for a solution:

  1. create a new phpmethod named whatever you want, insert this code:

    indent preformatted text by 4 spaces
    //<?php
    $searchLocation = ‘New York’;

$searchQuery = http_build_query([
‘access_key’ => ‘Your_API_Key’,
‘query’ => $searchLocation,
]);

$httpRequest = curl_init(sprintf(’%s?%s’, ‘http://api.weatherstack.com/current’, $searchQuery));
curl_setopt($httpRequest, CURLOPT_RETURNTRANSFER, true);

$responseData = curl_exec($httpRequest);
curl_close($httpRequest);

$result = json_decode($responseData, true);

//echo “Current temperature in $searchLocation is {$result[‘current’][‘temperature’]}℃”, PHP_EOL;
{weathertemp} = “{$result[‘current’][‘temperature’]}”;
{cloudcover} = “{$result[‘current’][‘cloudcover’]}”;
{humidity} = “{$result[‘current’][‘humidity’]}”;
{wicon} = “{$result[‘current’][‘weather_icons’]}”;
//?>

create your fields and add the phpmethod to load on record or validate,

I am trying to get the weather icon to show in an html image filed though {wicon}, not sure how to achieve that. result returned is a url link to the icon, hmmm…

lol not sure if anyone cares but i figured out how to load the icon as an image, make the field a label and then implode the url because its an array, so add something like:

> $wicon1 = implode(($result['current']['weather_icons']));
> {wicon} = "<Img src =$wicon1>";

New error though, i seem to be getting a 403 page: Forbidden when deploying to production…

I cant think it would be anything related to Weatherstack as the api is called on record load and not validate, where as the 403 page occurs after saving.

any ideas please?

How are you displaying it? I do things where the page autoloader from a webserver without having to use the menu