Last active
June 26, 2016 00:37
-
-
Save jpcercal/642d5ed9b7e0e404d0af to your computer and use it in GitHub Desktop.
An Example of PHP SoapClient
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // Nota: O ideal é que esse script fique em outro arquivo para | |
| // não misturar regras de negócio com o HTML ... | |
| $wsdl = "http://ws.registrocivil.org.br/listarCartoriosReceitaFederal.cfc?wsdl"; | |
| try { | |
| $options = array( | |
| 'soap_version' => SOAP_1_1, | |
| 'exceptions' => true, | |
| 'cache_wsdl' => WSDL_CACHE_NONE, | |
| ); | |
| $soap = new SoapClient($wsdl, $options); | |
| $xml = $soap->listarCartoriosReceitaFederal(); | |
| $document = new SimpleXMLElement(trim($xml)); | |
| $data = array( | |
| 'cartorios' => array(), | |
| 'estados' => array(), | |
| ); | |
| foreach ($document->cartorios->cartorio as $cartorio) { | |
| $data['cartorios'][] = array( | |
| 'nome' => (string) utf8_decode($cartorio->nome), | |
| 'oficial' => (string) utf8_decode($cartorio->oficial), | |
| 'endereco' => (string) utf8_decode($cartorio->endereco), | |
| 'cidade' => (string) utf8_decode($cartorio->cidade), | |
| 'cep' => (string) utf8_decode($cartorio->cep), | |
| 'estado' => (string) utf8_decode($cartorio->estado), | |
| 'telefone' => (string) utf8_decode($cartorio->telefone), | |
| 'num_cnpj' => (int) utf8_decode($cartorio->num_cnpj), | |
| 'quantidade_cpf_emitidos' => (int) utf8_decode($cartorio->quantidade_cpf_emitidos), | |
| ); | |
| } | |
| $index = 0; | |
| foreach ($document->estados->uf as $uf) { | |
| $uf = (string) utf8_decode($uf); | |
| $qtd = (int) utf8_decode($document->estados->quantidade_cpf_emitidos[$index]); | |
| $data['estados'][$uf] = $qtd; | |
| $index++; | |
| } | |
| $registros = json_encode($data); | |
| } catch (Exception $e) { | |
| die($e->getMessage()); | |
| } | |
| ?><!DOCTYPE html> | |
| <html> | |
| <head> | |
| <title>WSDL</title> | |
| <script type="text/javascript"> | |
| // O ideial é carregar o JSON através de um endpoint público | |
| // que poderia ficar no mesmo servidor e apenas devolver os | |
| // dados do WSDL parseados. | |
| var registros = <?php echo $registros ?>; | |
| // Imprime os registros no Console do navegador ... | |
| console.log(registros); | |
| </script> | |
| </head> | |
| <body> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment