Recieve a Json string created by website

General Help regarding HMG, Compilation, Linking, Samples

Moderator: Rathinagiri

Post Reply
User avatar
zolysoftsolutions
Posts: 139
Joined: Wed Feb 29, 2012 3:33 am
Location: Gyulakuta, Erdélyország
Contact:

Recieve a Json string created by website

Post by zolysoftsolutions »

Hi my friends,

On a website named www.zolano.ro it happens something, saved into a json string.
I can transfer this data by calling from another website trough this code in php:

Code: Select all

$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);                                                                  
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($curl, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data))                                                                       
);
curl_setopt($curl, CURLOPT_URL, 'http://www.zolano.ro/');  // The url path we want to call
$result = curl_exec($curl);

//see the results
$valami=json_decode($result,true);
curl_close($curl);
foreach ($valami as $xxx) {        //is arrays in array ...so is a table content with 3 columns
  foreach ($xxx as $yyy) {	
   print_r($yyy[0]); echo "  ";
   print_r($yyy[1]); echo "  ";
   print_r($yyy[2]); echo "<br>";
  };
 };
My question is if I can do this also in a HMG application installed on a pc connected to internet?
I have to mention that I checked the sample of master Rathinagiri, but there I have to call/recieve the string, not just to read from a file with .json extension.

Normally I can save the json string into a file in this way ...

Code: Select all

$fp = fopen('results.json', 'w');
fwrite($fp, json_encode($result,true));
fclose($fp);
..and to download in HMG app trough FTP. Exist a simpler way?

Best regards,
Zoli B.
_______________________________
Open eyes for you!
edk
Posts: 909
Joined: Thu Oct 16, 2014 11:35 am
Location: Poland

Re: Recieve a Json string created by website

Post by edk »

Code: Select all

#include "hmg.ch"

Function Main()
Local cURL := "http://www.zolano.ro/"
Local cPOSTdata
LOCAL cResp, hRest, oWeb

//Init
BEGIN SEQUENCE WITH {|o| break(o)}
	oWeb := Win_OleCreateObject( "MSXML2.ServerXMLHTTP" )

RECOVER
     MsgStop( "Microsoft XML Core Services (MSXML) 6.0 is not installed."+CRLF+;
          "Download and install MSXML 6.0 from http://msdn.microsoft.com/xml"+CRLF+;
          "before continuing.")
     oWeb:=""

END SEQUENCE

IF EMPTY(oWeb)
	MsgStop("Error while init.")
	RETURN 
ENDIF

cResp := SendRequest( cUrl, cPOSTdata, oWeb )
	
IF cResp = "!ERROR!"
	MsgStop( cResp )	/* Report any errors */
ELSE
	hResp := hb_jsonDecode( cResp )
	MsgDebug( cResp )
	MsgDebug( hResp )
ENDIF

//Close
oWeb:Abort()

RETURN 
*****************************************************************************************
Function SendRequest (cUrl, cPOSTdata, oWeb)
Local cReturn
Default cPOSTdata:=''

BEGIN SEQUENCE WITH {|o| break(o)}
	oWeb:Open( "POST", cUrl, .F. )
	oWeb:Send( cPOSTdata )
	cReturn := oWeb:ResponseBody()

RECOVER USING oErr
	cReturn := "!ERROR!" + CRLF + oErr:Description
 
END SEQUENCE
 	
RETURN cReturn
*****************************************************************************************
User avatar
zolysoftsolutions
Posts: 139
Joined: Wed Feb 29, 2012 3:33 am
Location: Gyulakuta, Erdélyország
Contact:

Re: Recieve a Json string created by website

Post by zolysoftsolutions »

WOW !! Super. Thank you very much EDK.
I'ts amazing and it's working fine.

Thanks again,
Zoli B.
_______________________________
Open eyes for you!
User avatar
Rathinagiri
Posts: 5471
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Contact:

Re: Recieve a Json string created by website

Post by Rathinagiri »

Does this work for https websites?
edk wrote: Tue Nov 12, 2019 2:40 pm

Code: Select all

#include "hmg.ch"

Function Main()
Local cURL := "http://www.zolano.ro/"
Local cPOSTdata
LOCAL cResp, hRest, oWeb

//Init
BEGIN SEQUENCE WITH {|o| break(o)}
	oWeb := Win_OleCreateObject( "MSXML2.ServerXMLHTTP" )

RECOVER
     MsgStop( "Microsoft XML Core Services (MSXML) 6.0 is not installed."+CRLF+;
          "Download and install MSXML 6.0 from http://msdn.microsoft.com/xml"+CRLF+;
          "before continuing.")
     oWeb:=""

END SEQUENCE

IF EMPTY(oWeb)
	MsgStop("Error while init.")
	RETURN 
ENDIF

cResp := SendRequest( cUrl, cPOSTdata, oWeb )
	
IF cResp = "!ERROR!"
	MsgStop( cResp )	/* Report any errors */
ELSE
	hResp := hb_jsonDecode( cResp )
	MsgDebug( cResp )
	MsgDebug( hResp )
ENDIF

//Close
oWeb:Abort()

RETURN 
*****************************************************************************************
Function SendRequest (cUrl, cPOSTdata, oWeb)
Local cReturn
Default cPOSTdata:=''

BEGIN SEQUENCE WITH {|o| break(o)}
	oWeb:Open( "POST", cUrl, .F. )
	oWeb:Send( cPOSTdata )
	cReturn := oWeb:ResponseBody()

RECOVER USING oErr
	cReturn := "!ERROR!" + CRLF + oErr:Description
 
END SEQUENCE
 	
RETURN cReturn
*****************************************************************************************
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.
edk
Posts: 909
Joined: Thu Oct 16, 2014 11:35 am
Location: Poland

Re: Recieve a Json string created by website

Post by edk »

For me it works with https with the TLS 1.2 protocol http://hmgforum.com/viewtopic.php?p=66777#p66777
Post Reply