SoapClient无法在PHP中工作,但Web Service工具可以工作

时间:2019-03-12 15:44:43

标签: php xml web-services soap asmx

使用以下代码:

$client = new \SoapClient(
    'http://ws.correios.com.br/calculador/CalcPrecoPrazo.asmx?WSDL',
    array(
        // Stuff for development.
        'trace' => 1,
        'exceptions' => true,
        'cache_wsdl' => WSDL_CACHE_NONE

    )
);

$data['nCdServico'] = '04510';
$data['sCepOrigem'] = '14400500';
$data['sCepDestino'] = '14400500';
$data['nVlPeso'] = '1';
$data['nCdFormato'] = 1;
$data['nVlComprimento'] = 17;
$data['nVlAltura'] = 17;
$data['nVlLargura'] = 17;
$data['nVlDiametro'] = 17;
$data['nVlValorDeclarado'] = 0;


$response = $client->CalcPrecoPrazo($data);

我正在使用外部Web服务,并且在请求请求时,向我发送意外错误:“对象未设置为对象的实例”。显然我的代码有问题,因为当我使用SoapUI时,它可以工作。如果删除$ data ['nVlLargura'],它会抱怨nVLLargura丢失,因此我认为它正在接收参数。我对此事在Web服务和xml方面没有太多经验。

当我使用SoapUI发送此xml时,它会起作用:

   <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
   <soapenv:Header/>
   <soapenv:Body>
      <tem:CalcPrecoPrazo>
         <!--Optional:-->
         <tem:nCdEmpresa>?</tem:nCdEmpresa>
         <!--Optional:-->
         <tem:sDsSenha>?</tem:sDsSenha>
         <!--Optional:-->
         <tem:nCdServico>40010</tem:nCdServico>
         <!--Optional:-->
         <tem:sCepOrigem>14400459</tem:sCepOrigem>
         <!--Optional:-->
         <tem:sCepDestino>14400500</tem:sCepDestino>
         <!--Optional:-->
         <tem:nVlPeso>2</tem:nVlPeso>
         <tem:nCdFormato>1</tem:nCdFormato>
         <tem:nVlComprimento>17</tem:nVlComprimento>
         <tem:nVlAltura>17</tem:nVlAltura>
         <tem:nVlLargura>17</tem:nVlLargura>
         <tem:nVlDiametro>0</tem:nVlDiametro>
         <!--Optional:-->
         <tem:sCdMaoPropria>?</tem:sCdMaoPropria>
         <tem:nVlValorDeclarado>0</tem:nVlValorDeclarado>
         <!--Optional:-->
         <tem:sCdAvisoRecebimento></tem:sCdAvisoRecebimento>

      </tem:CalcPrecoPrazo>
   </soapenv:Body>
</soapenv:Envelope>

1 个答案:

答案 0 :(得分:0)

尝试使用下一个脚本执行SOAP请求。功能和类型列表仅供参考,请在测试后将其删除:

<?php
// SOAP
$soap = new SoapClient(
    'http://ws.correios.com.br/calculador/CalcPrecoPrazo.asmx?WSDL',
    array(
        // Stuff for development.
        'trace' => 1,
        'exceptions' => true,
        'cache_wsdl' => WSDL_CACHE_NONE

    )
);

// List functions
echo 'Functions: '.'</br>';
$functions = $soap->__getFunctions();
foreach($functions as $item) {
    echo $item.'</br>';
}
echo '</br>';

// List types
echo 'Types: '.'</br>';
$types = $soap->__getTypes();
foreach($types as $item) {
    echo $item.'</br>';
}
echo '</br>';

// Consume SOAP
$params = array(
    "nCdEmpresa" => "?",
    "sDsSenha" => "?",
    "nCdServico" => "40010",
    "sCepOrigem" => "14400459",
    "sCepDestino" => "14400500", 
    "nVlPeso" => "2",
    "nCdFormato" => 1,
    "nVlComprimento" => 17,
    "nVlAltura" => 17, 
    "nVlLargura" => 17, 
    "nVlDiametro" => 0, 
    "sCdMaoPropria" => "?",
    "nVlValorDeclarado" => 0,
    "sCdAvisoRecebimento" => ""
);   
$responce = $soap->CalcPrecoPrazo($params);
var_dump($responce);
echo '<br>';
?>
相关问题