NFSe - Brasilia PHP

Ah bom!

Eu tenho esse código em C#:


        public string SendRequest(string data, string action)
        {
            try
            {
                WebRequestHandler handler = new WebRequestHandler();
                if (this.Certificado != null)
                    handler.ClientCertificates.Add(this.Certificado);
                HttpClient client = new HttpClient(handler);
                client.DefaultRequestHeaders.ExpectContinue = false;
                client.DefaultRequestHeaders.Add("Accept", "*/*");
                //client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/soap+xml"));
                //client.DefaultRequestHeaders.Add("SOAPAction", action);
                client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; MS Web Services Client Protocol 2.0.50727.8806)");
                client.DefaultRequestHeaders.TransferEncodingChunked = null;

                StringContent queryString = new StringContent(data, Encoding.UTF8, "text/xml");
                HttpResponseMessage response = client.PostAsync(new Uri(this.Url), queryString).Result;
                String responseValue = string.Empty;
                Stream responseStream = null;


                /*
                if (response != null)
                {
                    //response.EnsureSuccessStatusCode();
                    this.cLastCode = response.StatusCode;

                    Task task = response.Content.ReadAsStringAsync().ContinueWith(t =>
                    {
                        responseValue = t.Result;
                    });

                    task.Wait();
                };
                */

                if (response != null)
                {
                    Task task = response.Content.ReadAsStreamAsync().ContinueWith(t =>
                    {
                        responseStream = t.Result;
                    });
                    task.Wait();

                    string encoding = String.Join(",", response.Content.Headers.ContentEncoding);

                    if (response.Content.Headers.ContentEncoding.Contains("gzip"))
                        responseStream = new GZipStream(responseStream, CompressionMode.Decompress);
                    else if (response.Content.Headers.ContentEncoding.Contains("deflate"))
                        responseStream = new DeflateStream(responseStream, CompressionMode.Decompress);

                    using (StreamReader streamReader = new StreamReader(responseStream))
                    {
                        responseValue = streamReader.ReadToEnd();
                    }

                }

                return responseValue;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public string ConsultarLoteRps(string CNPJ, string IM, string Protocolo)
        {
            String NfseDados = String.Format(
                "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                "<ConsultarLoteRpsEnvio xmlns=\"http://www.issnetonline.com.br/webserviceabrasf/vsd/servico_consultar_lote_rps_envio.xsd\" " +
                                         " xmlns:tc=\"http://www.issnetonline.com.br/webserviceabrasf/vsd/tipos_complexos.xsd\">" +
                "<Prestador>" +
                "<tc:CpfCnpj>" +
                "<tc:Cnpj>{0}</tc:Cnpj>" +
                "</tc:CpfCnpj>" +
                "<tc:InscricaoMunicipal>{1}</tc:InscricaoMunicipal>" +
                "</Prestador>" +
                "<Protocolo>{2}</Protocolo>" +
                "</ConsultarLoteRpsEnvio>", CNPJ, IM, Protocolo);

            String s = String.Format(
                "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                "<S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
                "<S:Body>" +
                "<ConsultarLoteRps xmlns=\"http://www.issnetonline.com.br/webservice/nfd\">" +
                "<xml>{0}</xml>" +
                "</ConsultarLoteRps>" +
                "</S:Body></S:Envelope>",
                NfseDados);


            File.WriteAllText(cFileName.Replace(".xml", ".soap.xml"), s);

            return this.SendRequest(s, "http://www.issnetonline.com.br/webservice/nfd/ConsultarLoteRps");
        }

        public String ConsultarNfsePorRps(String CNPJ, String IM, String RPS, String SERIE)
        {
            String NfseDados = String.Format(
                "&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;" +
                "&lt;ConsultarNfseRpsEnvio xmlns=\"http://www.issnetonline.com.br/webserviceabrasf/vsd/servico_consultar_nfse_rps_envio.xsd\" " +
                                         " xmlns:tc=\"http://www.issnetonline.com.br/webserviceabrasf/vsd/tipos_complexos.xsd\"&gt;" +
                "&lt;IdentificacaoRps&gt;" +
                "&lt;tc:Numero&gt;{0}&lt;/tc:Numero&gt;" +
                "&lt;tc:Serie&gt;{1}&lt;/tc:Serie&gt;" +
                "&lt;tc:Tipo&gt;1&lt;/tc:Tipo&gt;" +
                "&lt;/IdentificacaoRps&gt;" +
                "&lt;Prestador&gt;" +
                "&lt;tc:CpfCnpj&gt;&lt;tc:Cnpj&gt;{2}&lt;/tc:Cnpj&gt;&lt;/tc:CpfCnpj&gt;" +
                "&lt;tc:InscricaoMunicipal&gt;{3}&lt;/tc:InscricaoMunicipal&gt;" +
                "&lt;/Prestador&gt;" +
                "&lt;/ConsultarNfseRpsEnvio&gt;", RPS, SERIE, CNPJ, IM);


            String s = String.Format(
                "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
                "<soap:Body>" +
                "<ConsultarNFSePorRPS xmlns =\"http://www.issnetonline.com.br/webservice/nfd\">" +
                "<xml>{0}</xml>" +
                "</ConsultarNFSePorRPS>" +
                "</soap:Body>" +
                "</soap:Envelope>",
                NfseDados);

            File.WriteAllText(cFileName.Replace(".xml", ".soap.xml"), s);

            return this.SendRequest(s, "http://www.issnetonline.com.br/webservice/nfd/ConsultarNFSePorRPS");
        }