Muito boa as dicas, já uso CreateObject("MSXML2.ServerXMLHTTP"), inclusive pra consumir API de consulta de dados de empresas passando CNPJ.
Código: Selecionar todos
////////////////////////////////////////////////////////////////////////////////
//https://pctoledo.org/forum/viewtopic.php?f=43&p=155455#p155455
//https://pctoledo.org/forum/viewtopic.php?f=4&t=23628 (com Header)
//https://pctoledo.org/forum/viewtopic.php?f=4&t=17427 (usando a hb_JSONDecode( cJSON, @hJSON ) )
//https://nfe.io/docs/desenvolvedores/rest-api/consulta-de-cnpj-v1/ (para consultas)
//https://pctoledo.org/forum/viewtopic.php?f=4&p=148594#p148594 (pode fazer FTP)
Function CNPJ_Consulta( cCnpj )
Local mSite := "http://receitaws.com.br/v1/cnpj/", oHttp, hCNPJA := Hash(), mJSONCNPJ, hJSON := Hash()
//mSite := "https://publica.cnpj.ws/cnpj/" //mais completo
If Len( cCnpj ) <> 14
REturn NIL
Endif
Begin Sequence
Try
oHttp := CreateObject("MSXML2.ServerXMLHTTP")
oHttp:Open("GET", mSite + cCNPJ, .F.)
oHttp:send()
CATCH oError
msg := 'Internet Error: 010' + CRLF
msg += 'Aviso..: Erro verificado ao fazer requisição de CNPJ no Site indicado.' + HB_Eol()
msg += 'Site...:' + mSite + HB_Eol()
msg += 'CNPJ...:' + cCNPJ + HB_Eol()
msg += cl_getError(oError, .t.)
msgError ( msg )
hCNPJA := NIL
Break
End
If Empty( oHttp:responseText )
hCNPJA := NIL
Break
ElseIf '"ERROR"' $ oHttp:responseText
msg := 'Request error: 015' + CRLF
msg += 'Aviso..: O retorno da consulta de CNPJ está com erros.' + HB_Eol()
msg += '- Aguarde um minuto e tente novamente.' + HB_Eol()
msg += 'Site...:' + mSite + HB_Eol()
msg += 'CNPJ...:' + cCNPJ + HB_Eol()
msg += 'Response:' + oHttp:responseText + HB_Eol()
msg += 'Status..:' + Str(oHttp:status, 3) + HB_Eol()
msg += cl_getError(, .t.)
MsgError ( msg, .t. )
hCNPJA := NIL
Break
ElseIf 'Too many' $ oHttp:responseText
msg := 'Request error: 020' + CRLF
msg += 'Aviso..: Muitas tentativas de pesquisar CNPJ no Site indicado.' + HB_Eol()
msg += '- Aguarde um minuto e tente novamente.' + HB_Eol()
msg += 'Site...:' + mSite + HB_Eol()
msg += 'CNPJ...:' + cCNPJ + HB_Eol()
msg += 'Response:' + oHttp:responseText + HB_Eol()
msg += 'Status..:' + Str(oHttp:status, 3) + HB_Eol()
MsgAtencao ( msg )
hCNPJA := NIL
Break
Endif
mJSONCNPJ := HB_AnsiToOem(oHttp:responseText)
//ADOStringGrava(mJSONCNPJ + CRLF)
hCNPJA := JSontoHash( mJSONCNPJ )
oHttp := NIL
End Sequence
Return hCNPJA
/////////////////////////////////////////////////////////////////////////////
//https://www.linguagemclipper.com.br/aprendendo-hash
Function JSonToHash( cStringJson )
/***
* Converte string formato Json em Hash
*/
Local hJson := {=>}
cStringJson := StrTran( cStringJson, ':[','=>{')
cStringJson := StrTran( cStringJson, '":"','" => "')
cStringJson := StrTran( cStringJson, '[','{')
cStringJson := StrTran( cStringJson, ']','}')
cStringJson := StrTran( cStringJson, '":null','"=>nil')
cStringJson := StrTran( cStringJson, '":true' ,'"=>.t.' )
cStringJson := StrTran( cStringJson, '":false','"=>.f.')
cStringJson := StrTran( cStringJson, '":','"=>')
cStringJson := StrTran( cStringJson, "\/","/" )
hJSon := &( cStringJson )
Return hJson
/////////////////////////////////////////////////////////////////////////////
Function cl_LimpaString( cString )
Local mRetorno
mRetorno := StrTran( cString, '.', '' )
mRetorno := StrTran( mRetorno, '-', '' )
mRetorno := StrTran( mRetorno, '/', '' )
mRetorno := StrTran( mRetorno, '\', '' )
mRetorno := StrTran( mRetorno, ',', '' )
Return mRetorno
/////////////////////////////////////////////////////////////////////////////
Function cl_NulToString( cString )
cString := If(cString = NIL, '', cString )
Return cString