Page 1 of 1
Invoke a api / webservice.
Posted: Sat Jan 26, 2013 3:48 am
by PedroAlex
How invoke a webservice from xBase / eXpress.
For example to invoke this webservice
http://www.nif.pt/?json=1&q=503949574
'503949574' is a Tax Number and the webservice returns company data.
Any sample helps.
Thanks in advance.
Respects.
Pedro Alex.
Re: Invoke a api / webservice.
Posted: Sat Jan 26, 2013 8:10 pm
by rdonnay
Code: Select all
DC_SpawnUrl("http://www.nif.pt/?json=1&q=503949574")
Re: Invoke a api / webservice.
Posted: Mon Jan 28, 2013 8:49 am
by PedroAlex
Thanks for feedBack
Invoke a webservice means to send and receive data from a website without opening the explorer.
This command opens internet explorer and shows the desired information.
But this process has to be done without opening the explorer.
Is this possible!?
Respects
Pedro Alex.
Re: Invoke a api / webservice.
Posted: Mon Jan 28, 2013 10:03 am
by skiman
Re: Invoke a api / webservice.
Posted: Mon Jan 28, 2013 12:57 pm
by Tom
3 lines of code with Boris Borzic's Xb2.Net.
http://www.xb2.net/menu.htm
Re: Invoke a api / webservice.
Posted: Tue Jan 29, 2013 7:07 am
by Cliff Wiernik
Hi Tom,
Just curious, what are the 3 lines of code.
I have used xb2.net for several things and your code example would be helpful for comparison purposes as my experience is still somewhat limited.
Cliff.
Re: Invoke a api / webservice.
Posted: Tue Jan 29, 2013 7:47 am
by rdonnay
LoadFromUrl() is an Alaska ASINET function.
I use it a lot to retrieve webpage info and then parse it.
This is how I use it to retrieve an Excel file from the NYC TLC website.
Code: Select all
cUrl := "http://nyc.gov/html/tlc/downloads/excel/current_medallion_drivers.xls"
aDir := Directory(Current_Medallion_Drivers.Xls')
IF !Empty( cFileContents := GetTLCExcelFile( cUrl ) )
IF !Empty(aDir)
IF aDir[1,3] == Date()
cDate := DtoS(aDir[1,3]-1)
ELSE
cDate := DtoS(aDir[1,3])
ENDIF
FRename("Current_Medallion_Drivers.Xls", cFilePath + "\Medallion_Drivers_" + cDate + '.Xls' )
nHandle := FCreate( "Current_Medallion_Drivers.Xls" )
Fwrite( nHandle, cFileContents )
FClose( nHandle )
ENDIF
* --------------
STATIC FUNCTION GetTLCExcelFile( cUrl )
LOCAL nPort := 80, cMethod := 'GET'
RETURN SendUrl( cUrl, nPort, cMethod, {} )
* --------------
STATIC FUNCTION SendUrl( cUrl, nPort, cMethod, aVars )
LOCAL i, cString := '', cResponse, nProtocol
IF 'HTTPS:' $ Upper(cUrl)
nProtocol := INTERNET_COMMUNICATION_SECURE
nPort := INTERNET_DEFAULT_HTTPS_PORT
ELSE
nProtocol := INTERNET_COMMUNICATION_PUBLIC
nPort := INTERNET_DEFAULT_HTTP_PORT
ENDIF
IF nPort == 0
nPort := nil
ENDIF
FOR i := 1 TO Len(aVars)
IF Empty(aVars[i,1])
EXIT
ENDIF
IF i > 1
cString += '&'
ENDIF
cString += Alltrim(aVars[i,1]) + '="' + Alltrim(aVars[i,2]) + '"'
NEXT
IF cMethod == 'GET' .AND. !Empty(cString)
cUrl += '/?' + cString
cString := nil
ENDIF
cResponse := LoadFromUrl( cUrl, nPort, nProtocol, , , cMethod, cString )
IF Empty(cResponse)
cResponse := ''
ENDIF
RETURN cResponse
Re: Invoke a api / webservice.
Posted: Wed Jan 30, 2013 1:00 am
by skiman
Hi,
If you don't have a professional subscription, you don't have access to asinet. The loadfromurl from Phil Ide is a replacement for this.
Re: Invoke a api / webservice.
Posted: Wed Jan 30, 2013 2:41 am
by PedroAlex
Many thanks for your topics.
I will investigate.
Respects
Pedro Alex.
Re: Invoke a api / webservice.
Posted: Wed Jan 30, 2013 5:18 am
by Tom
Hi, Cliff.
Just curious, what are the 3 lines of code.
Okay, eight lines.
Code: Select all
* include xb2net.ch, pragma xb2net.lib - if not already done
STATIC FUNCTION GetContent(cUrl)
LOCAL oHttp, oResponse, oForm, cContent
oHttp := xbHTTPClient():New()
oForm := xbForm():New()
oForm:SetVar( ... ) // if needed
oResponse := oHttp:Execute(cUrl,,oForm)
cContent := oResponse:Content
* destroy objects (not really needed)
RETURN cContent