Jump to content
Fivewin Brasil

Pegar os dados do CNPJ por esse site


dorneles

Recommended Posts

Bom dia!

Gostei da API! E é de graça! :)

Tá na mão:

********************************************

Function GetDadosCNPJ()
    * retorna dados de um CNPJ
    * retorno no formato JSON
    * site que oferece o servico: https://www.receitaws.com.br
    
    Local oServerWS, cUrl, aArray, oCnpj:= SPACE(14)
    
    MsgGet("ReceitaWS", "Informe o CNPJ:", @oCnpj)
    
    cUrl:= "https://www.receitaws.com.br/v1/cnpj/"+Alltrim(oCnpj)

    Try
        oServerWS := CreateObject( 'MSXML2.ServerXMLHTTP.6.0' )  
    Catch e
        ? e:Description
        return nil
    End 
    
    oServerWS:open('GET', cUrl, .f.)
    oServerWS:setRequestHeader('Content-Type'    , 'text/xml; charset=utf-8')    // Obtém ou define o valor de cabeçalho de Content-type HTTP.

    oServerWS:send()    
    
    * Verifica o status do request. Exibe erro se houver
    IF oServerWS:Status != 200 // OK
       MsgStop( Alltrim(STR(oServerWS:Status)) +" - "+ oServerWS:StatusText , "Erro")
        RETURN NIL
    ENDIF
    
    WHILE oServerWS:readyState != 4
       oServerWS:waitForResponse(1000)
    END   
        
    * decodifica o arquivo json e transforma em um array multidimensional
   x :=  hb_jsondecode( oServerWS:responseText, @aArray )
    
    if aArray == nil
        MsgStop("Erro ao retornar os dados. Tente novamente.")
        return nil
    endif

    if aArray['status'] != 'OK' // exibe erros
        MsgStop(aArray['message'], 'Erro')
       return nil
    endif
    
    xbrowse(aArray) // mostra o resultado    
    
return nil

 

 

 

Link to comment
Share on other sites

  • 2 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...