Localizar('81170752'); print_r($CEP); */ function ConvetMaiuscula($string) { if ( $string == '' ) return ''; else $string = strtoupper($string); return $string; } function ConvetNumero($string) { return ereg_replace('[^0-9]', '', $string); } class BuscaCep { private $dados = array(); public function Localizar($cep) { $this->dados['cep'] = ConvetNumero($cep); if ( strlen($this->dados['cep']) != 8 ) exit("CEP INVALIDO"); else $this->Processa(); return $this->dados; } private function Processa() { if ( $this->WebService1() == true ) return $this->dados; else if ( $this->WebService2() == true ) return $this->dados; else if ( $this->WebService3() == true ) return $this->dados; else exit("NAO FOI POSSIVEL ENCONTRAR O CEP!"); } private function WebService1() { if(!get_extension_funcs('soap')) return false; $client = new SoapClient(NULL, array( "location" => "http://www.byjg.com.br/xmlnuke-php/webservice.php/ws/cep", "uri" => "urn:xmethods-delayed-quotes", "style" => SOAP_RPC, "use" => SOAP_ENCODED ) ); $result = $client->__call( // Nome do método "obterLogradouro", // Parâmetros array( new SoapParam( // CEP informado $this->dados['cep'], // Nome do parâmentro "parameters" ) ), // Opções array( "uri" => "urn:xmethods-delayed-quotes", "soapaction" => "urn:xmethods-delayed-quotes#getQuote" ) ); if(strpos($result, utf8_encode('não encontrado')) !== false) return false; else { $retorno = explode(', ', $result); $retorno = array_map('trim', $retorno); } if ( count($retorno) != 4 ) return false; $this->dados['endereco'] = $retorno[0]; $this->dados['bairro'] = $retorno[1]; $this->dados['cidade'] = $retorno[2]; $this->dados['estado'] = $retorno[3]; return true; } private function WebService2() { $fp = @fsockopen("www.buscarcep.com.br" , 80, $errno, $errstr, 1); if(!$fp) return false; else { $query_url = "/?cep=".$this->dados['cep']."&formato=string"; $query_url .= " HTTP/1.0\r\n"; $query_url .= "Host: www.buscarcep.com.br\r\n"; $query_url .= "Connection: Close\r\n\r\n"; fwrite($fp, "GET " . $query_url); } $content = ''; while (! feof($fp) ) $content .= fgets($fp, 128); $responsecontent = explode("\r\n\r\n",$content); $resultado = $responsecontent[1]; parse_str($resultado, $resultado_busca); if (empty($resultado_busca)) return false; else if ($resultado_busca['resultado'] != '1') return false; $this->dados['endereco'] = $resultado_busca['tipo_logradouro'] .' '. $resultado_busca['logradouro']; $this->dados['bairro'] = $resultado_busca['bairro']; $this->dados['cidade'] = $resultado_busca['cidade']; $this->dados['estado'] = $resultado_busca['uf']; return true; } private function WebService3() { $fp = @fsockopen ("republicavirtual.com.br" , 80, $errno, $errstr, 3); if(!$fp) return false; else { $query_url = "/web_cep.php?cep=".$this->dados['cep']."&formato=query_string"; $query_url .= " HTTP/1.0\r\n"; $query_url .= "Host: republicavirtual.com.br\r\n"; $query_url .= "Connection: Close\r\n\r\n"; fwrite($fp, "GET " . $query_url); } $content = ''; while (! feof($fp) ) $content .= fgets($fp, 128); $responsecontent = explode("\r\n\r\n",$content); $resultado = $responsecontent[1]; parse_str($resultado, $resultado_busca); if (empty($resultado_busca)) return false; else if ($resultado_busca['resultado'] != '1') return false; else if (strpos($resultado_busca['logradouro'],'XXX')) return false; $this->dados['endereco'] = $resultado_busca['tipo_logradouro'] .' '. $resultado_busca['logradouro']; $this->dados['bairro'] = $resultado_busca['bairro']; $this->dados['cidade'] = $resultado_busca['cidade']; $this->dados['estado'] = $resultado_busca['uf']; return true; } } ?>