First post (I am probably using the wrong terms, be gentle),
I have tried some searches, but not knowing what terms to use, I am coming up empty.
I am trying to use the API to pull data into a MariaDB. I have done this with a different weather station manufactures API, so I know it works.
When I try to read the string and assign it to variables, I don’t get the expected results. It must be how I have things in my foreach loop formatted. (here is a snippet of the code in question:)
$apiString = 'https://swd.weatherflow.com/swd/rest/observations/station/[sanitized_station_id]?token=[sanitized_token]';
$json = file_get_contents($apiString);
//convert json object to php associative array
$data = json_decode($json, true);
// Loop through the array
foreach ($data as $row)
{
$station_id = $row['station_id'];
$sql = "INSERT INTO $tableName
(
`Station_Name`
)
VALUES
(
'$station_id'
)";
echo "$sql<br />";
}
(The echo statement gives me the following output:)
INSERT INTO TempestWeatherData ( Station_Name
) VALUES ( ‘’ )
INSERT INTO TempestWeatherData ( Station_Name
) VALUES ( ‘L’ )
INSERT INTO TempestWeatherData ( Station_Name
) VALUES ( ‘L’ )
INSERT INTO TempestWeatherData ( Station_Name
) VALUES ( ‘’ )
INSERT INTO TempestWeatherData ( Station_Name
) VALUES ( ‘’ )
INSERT INTO TempestWeatherData ( Station_Name
) VALUES ( ‘A’ )
INSERT INTO TempestWeatherData ( Station_Name
) VALUES ( ‘’ )
INSERT INTO TempestWeatherData ( Station_Name
) VALUES ( ‘’ )
INSERT INTO TempestWeatherData ( Station_Name
) VALUES ( ‘’ )
INSERT INTO TempestWeatherData ( Station_Name
) VALUES ( ‘’ )
INSERT INTO TempestWeatherData ( Station_Name
) VALUES ( ‘’ )
INSERT INTO TempestWeatherData ( Station_Name
) VALUES ( ‘’ )
I should only have one “Insert Into” statement (not 12) and the “Value” should have the ‘station_id’ rather that an empty value.
Can anybody point me in the right direction to get my “foreach” section formatted correctly?
Maybe some code examples.
Thanks!
Scott