https (webservice) requisition from the São Paulo Public Treasury

Asked

Viewed 122 times

4

I’m trying to make a request https the Webservice of the São Paulo Public Treasury, but I don’t know where I’m going wrong.

This is the address:

http://www.fazenda.sp.gov.br/contas/webservice.shtm

According to the guidance of manual, no authentication is required.

Basically, I want the committed expense of a given year. My code is this one:

library(XML)
library(httr)
a<-GET('https://webservices.fazenda.sp.gov.br/WSTransparencia/TransparenciaServico.asmx?',
   query = list(op="ConsultarDespesas",
                ano="2015",
                flagCredor=0,
                flagEmpenhado=1,
                flagLiquidado=0,
                flagPago=0))
b<-xmlParse(a)

I get the following error:

Erro: 1: Opening and ending tag mismatch: p line 52 and br
2: Opening and ending tag mismatch: span line 49 and br
3: EntityRef: expecting ';'
4: Opening and ending tag mismatch: br line 43 and p
5: AttValue: " or ' expected
6: attributes construct error
7: Couldn't find end of Start Tag font line 85
8: Opening and ending tag mismatch: p line 85 and font
9: Opening and ending tag mismatch: span line 83 and p
10: AttValue: " or ' expected
11: attributes construct error
12: Couldn't find end of Start Tag font line 90
13: Opening and ending tag mismatch: pre line 87 and font
14: AttValue: " or ' expected
15: attributes construct error
16: Couldn't find end of Start Tag font line 97
17: Opening and ending tag mismatch: div line 41 and font
18: AttValue: " or ' expected
19: attributes construct error
20: Couldn't find end of Start Tag font line 98
21: Opening and ending tag mismatch: body line 39 and font
22: AttValue: " or ' expected
23: attributes construct error
24: Couldn't find end of

Can someone help me with that?

  • I already tried to do this request and could not. Here not error when I do b <-xml2::read_html(content(a, "text")). But I don’t know if that answers.

  • 1

    Hi Daniel, I solved the problem. I’ll put the answer below.

1 answer

2


I ended up partially solving (works only from 2010) the issue with the function curlPerfom() package RCurl. I took the example from here. From that, I got the information on the header and the body here and created a function to extract the expenses of a governmental body, whose code I obtain from here, 'Cause that’s all I needed.

Naturally, one can include other variables in the function and adjust the paste0(). I do not know if it is the best solution, but I am satisfied because I will be able to manipulate all expenses of São Paulo.

fazendaSP<-function(ano,codigo)
{

library(RCurl) 
library(XML)


  # O objeto (hd) abaixo contém contém o header 

hd<-c(Accept = "text/xml",
        Accept = "multipart/*",
        'Content-Type' = "text/xml; charset=utf-8",
        SOAPAction= "http://fazenda.sp.gov.br/wstransparencia/ConsultarDespesas"
        )

# O objeto (body) abaixo contém o body com as respectivas variáveis. Meu interesse está somente no código do órgão e no ano.
body<-paste0(
    '<?xml version="1.0" encoding="utf-8"?>
  <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
  <AutenticacaoHeader xmlns="http://fazenda.sp.gov.br/wstransparencia">
  <Usuario></Usuario>
  <Senha></Senha>
  </AutenticacaoHeader>
  </soap:Header>
  <soap:Body>
  <ConsultarDespesas xmlns="http://fazenda.sp.gov.br/wstransparencia">
  <ano>'
    ,ano,
    '</ano>
    <codigoOrgao>',
    codigo,
    '</codigoOrgao> # Quero somente a despesa do MP
      <codigoUo></codigoUo>
      <codigoUnidadeGestora></codigoUnidadeGestora>
      <codigoFonteRecursos></codigoFonteRecursos>
      <codigoTipoLicitacao></codigoTipoLicitacao>
      <codigoFuncao></codigoFuncao>
      <codigoSubfuncao></codigoSubfuncao>
      <codigoPrograma></codigoPrograma>
      <codigoAcao></codigoAcao>
      <codigoFuncionalProgramatica></codigoFuncionalProgramatica>
      <codigoMunicipio></codigoMunicipio>
      <codigoCategoria></codigoCategoria>
      <codigoGrupo></codigoGrupo>
      <codigoModalidade></codigoModalidade>
      <codigoElemento></codigoElemento>
      <naturezaDespesa></naturezaDespesa>
      <flagCredor>0</flagCredor>
      <cgcCpfCredor></cgcCpfCredor>
      <nomeCredor></nomeCredor>
      <flagEmpenhado>1</flagEmpenhado>
      <flagLiquidado>0</flagLiquidado>
      <flagPago>0</flagPago>
      </ConsultarDespesas>
      </soap:Body>
      </soap:Envelope>'
    )

h = basicTextGatherer() # A função curlPerfom não deposita os resultados em lugar algum,
# Eu preciso desse objeto (h) com funções que coletam o conteúdo.

### Agora estamos prontos, temos o header, o body, a função para coletar e, além disso, devido a um provavel
### malfuncionamento do ssl, pedir para ignorar a verificação 
curlPerform(url = "https://webservices.fazenda.sp.gov.br/WSTransparencia/TransparenciaServico.asmx?",
            httpheader = hd,
            postfields = body,
            .opts = list(ssl.verifypeer = FALSE),
            writefunction=h$update
)
a<-h$value()
b<-xmlParse(a)
c<-xmlToList(b)
d<-do.call("rbind",c$Body$ConsultarDespesasResponse$ConsultarDespesasResult$ListaItensDespesa)
e<-as.data.frame(d)
rownames(e)<-1:nrow(e)
return(e)
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.