public function request() { $this->checkPackageDimensions(); $param = array('nCdEmpresa' => $this->authCode, 'sDsSenha' => $this->authPass, 'nCdServico' => $this->service, 'sCepOrigem' => $this->zipFrom, 'sCepDestino' => $this->zipTo, 'nVlPeso' => $this->itemWeight, 'nCdFormato' => $this->itemForm, 'nVlComprimento' => $this->itemLength, 'nVlAltura' => $this->itemHeight, 'nVlLargura' => $this->itemWidth, 'nVlDiametro' => $this->itemDiameter, 'sCdMaoPropria' => $this->options['MaoPropria'], 'nVlValorDeclarado' => $this->options['DeclaredValue'], 'sCdAvisoRecebimento' => $this->options['Avis']); $webService = new SoapClient(self::URL); try { $result = $webService->CalcPrecoPrazo($param); $this->result = $result->CalcPrecoPrazoResult->Servicos->cServico; } catch (SoapFault $sf) { throw new Exception($sf->getCode() . ': ' . $sf->getMessage()); } }
<?php // ex_ws_soap.php try { $client = new SoapClient('http://ws.correios.com.br/calculador/CalcPrecoPrazo.asmx?WSDL', array('trace' => 1)); $frete = $client->CalcPrecoPrazo(array('nCdEmpresa' => '', 'sDsSenha' => '', 'nCdServico' => '41106', 'sCepOrigem' => 31030430, 'nCepDestino' => 35400000, 'nVlPeso' => 0.3, 'nCdFormato' => 1, 'nVlComprimento' => 30, 'nVlAltura' => 5, 'nVlLargura' => 20, 'nVlDiametro' => 0, 'sCdMaoPropria' => 'N', 'nVlValorDeclarado' => 0, 'sCdAvisoRecebimento' => 'N')); $result = $frete->CalcPrecoPrazoResult->Servicos->cServico; if ($result->MsgErro != '') { throw new Exception($result->MsgErro); } else { echo "Frete: " . $result->Valor . "<br />Prazo: " . $result->PrazoEntrega . " dia(s)"; } } catch (SoapFault $se) { echo 'Erro no Soap: ' . $se->getMessage(); } catch (Exception $e) { echo 'Erro: ' . $e->getMessage(); echo "<h1>REQUISIÇÃO:</h1><pre>"; echo $client->__getLastRequestHeaders(); echo htmlentities($client->__getLastRequest()); // xml echo "<h1>RESPOSTA:</h1>"; echo $client->__getLastResponseHeaders(); echo htmlentities($client->__getLastResponse()); // xml }