Question:
I'm trying to make an https request from the São Paulo Public Treasury Webservice, but I don't know where I'm going wrong.
This is the address:
http://www.fazenda.sp.gov.br/contas/webservice.shtm
As per the guidance of the manual , authentication is not 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 anyone help me with this?
Answer:
I ended up partially solving (only works as of 2010) the issue with curlPerfom()
function from RCurl
package. I took the example from here . From that, I got the information about the header and the body here and created a function to extract the expenses of a government agency, whose code I get from here , as that was all I needed.
Of course, one can include other variables in the function and adjust paste0()
. I don't know if it's the best solution, but I'm satisfied because I'll 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)
}