cURL Internet Access

Started by root, Today at 04:59 PM

Previous topic - Next topic

root

ScriptBasic provides a C API extension module for libcurl which offers HTML, REST and GraphQL direct access from your BOI scripts. This eliminates the need for third party emulations like ROI In-Synch.

This cURL example does a REST call to the Open Weather service and converts the JSON response to a ScriptBasic associative array. It displays the raw JSON and as a ScriptBasic array using the sbadump() debug function.

' OpenWeather - cURL & JSON Example
 
IMPORT curl.sbi
IMPORT webext.sbi
 
place = COMMAND()
 
ch = curl::init()
curl::option(ch, "URL", "http://api.openweathermap.org/data/2.5/weather?q=" & place & "&units=imperial&appid=APP KEY")
curl::option(ch, "CUSTOMREQUEST", "GET")
json = curl::perform(ch)
curl::finish(ch)
PRINT json,"\n\n"
web::json2sba(json)
web::sbadump(json)

PRINT "\nTempreture: ", json{"main"}{"temp"}, " F\n"
PRINT "Date: ", FORMATDATE("MM/DD/YEAR 0H:0m:0s",json{"dt"} + json{"timezone"}),"\n"

END

Output:

C:\ScriptBasic\examples>sbc weather.sb Lynden
{"coord":{"lon":-122.4521,"lat":48.9465},"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02d"}],"base":"stations","main":{"temp":65.07,"feels_like":63.77,"temp_min":60.51,"temp_max":67.69,"pressure":1023,"humidity":53,"sea_level":1023,"grnd_level":1015},"visibility":10000,"wind":{"speed":11.5,"deg":240},"clouds":{"all":20},"dt":1779061311,"sys":{"type":2,"id":2043940,"country":"US","sunrise":1779020706,"sunset":1779076053},"timezone":-25200,"id":5802035,"name":"Lynden","cod":200}

coord
  lon = -122.452
  lat = 48.9465
weather
  [1]
      id = 801
      main = Clouds
      description = few clouds
      icon = 02d
base = stations
main
  temp = 65.07
  feels_like = 63.77
  temp_min = 60.51
  temp_max = 67.69
  pressure = 1023
  humidity = 53
  sea_level = 1023
  grnd_level = 1015
visibility = 10000
wind
  speed = 11.5
  deg = 240
clouds
  all = 20
dt = 1779061311
sys
  type = 2
  id = 2043940
  country = US
  sunrise = 1779020706
  sunset = 1779076053
timezone = -25200
id = 5802035
name = Lynden
cod = 200

Tempreture: 65.07 F
Date: 5/17/2026 16:41:51

C:\ScriptBasic\examples>