https (webservice) request from São Paulo Public Finance

3

I'm trying to make a https request from the Public Finance Webservice in São Paulo, but I do not know where I'm going wrong.

This is the address:

link

According to the guidance of manual , it is not necessary to authenticate.

Basically, I want the expenditure committed for 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 anyone help me with this?

    
asked by anonymous 06.10.2016 / 21:21

1 answer

2

I have partially solved the problem with the curlPerfom() function of the RCurl package. I've taken the example from here example. From this, I got the information about header and body here and I created a function to extract the expenses of a governmental organ, whose code I get daqui , because that was all I needed.

Of course, the person can include other variables in the function and set paste0() . I do not know if it is the best solution, but I am satisfied because I will be able to handle all of São Paulo's expenses.

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)
}
    
09.10.2016 / 14:37