SoapClient不能在PHP中工作,但Web服务工具可以工作

问题描述 投票:0回答:1

使用此代码:

$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>
php xml web-services soap asmx
1个回答
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>';
?>
© www.soinside.com 2019 - 2024. All rights reserved.