如何使用 soapclient 创建此请求?

How to create this request with soapclient?

我需要从 WS SOAP 创建一个请求,但是我尝试设置参数并且总是 return 我犯了一个错误,有人可以帮助我并告诉我如何构建这个请求。

enter image description here

出于安全原因,我无法 post 某些真实值(WSDL、apikey、pass),但我使用带有真实值的 soapUI 测试了这个 WS,结果还不错。

我做了这些选择:

测试 01:

$wsdl = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$client = new SoapClient($wsdl,array('trace' => TRUE,'encoding' => 'UTF-8'));

$ArrayOfInt = array(
    'item'  => 0
);

$Cotizador_detalleEmpaques = array(
    'ubl'               => '0',
    'alto'              => '12',
    'ancho'             => '14',
    'largo'             => '20',
    'peso'              => '5',
    'unidades'          => '1'
);

$ArrayOfCotizador_detalleempaques = array(
    'item'              => new SoapVar($Cotizador_detalleEmpaques,SOAP_ENC_OBJECT,'item')
);


$Cotizador_cotizarIn = array(

    'nit'               =>  '890904713',
    'div'               =>  '01',
    'cuenta'            =>  '3',
    'producto'          =>  '23',
    'origen'            =>  '11001000',
    'destino'           =>  '05001000',
    'valoracion'        =>  '50000',
    'nivel_servicio'    =>  new SoapVar($ArrayOfInt,SOAP_ENC_OBJECT,'nivel_servicio'),
    'detalle'           =>  new SoapVar($ArrayOfCotizador_detalleempaques,SOAP_ENC_OBJECT,'detalle'),
    'apikey'            =>  'xxxxxxxxxxxxxxxxxxxxxxxx',
    'clave'             =>  'xxxxxxxxxxxxxxxx'

);

$response = $client->Cotizador_cotizar(new SoapVar($Cotizador_cotizarIn,SOAP_ENC_OBJECT,'p'));

测试 02:

$wsdl = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$client = new SoapClient($wsdl,array('trace' => TRUE,'encoding' => 'UTF-8'));


$ArrayOfInt = array(

    'item'  => new SoapVar(0,XSD_INT,'ubl')

);


$Cotizador_detalleEmpaques = array(

    'ubl'               => new SoapVar('0',XSD_STRING,'ubl'),
    'alto'              => new SoapVar('12',XSD_STRING,'alto'),
    'ancho'             => new SoapVar('14',XSD_STRING,'ancho'),
    'largo'             => new SoapVar('20',XSD_STRING,'largo'),
    'peso'              => new SoapVar('5',XSD_STRING,'peso'),
    'unidades'          => new SoapVar('1',XSD_STRING,'unidades')

);


$ArrayOfCotizador_detalleempaques = array(

    'item'              => new SoapVar($Cotizador_detalleEmpaques,SOAP_ENC_OBJECT,'item')

);


$Cotizador_cotizarIn = array(

    'nit'               =>  new SoapVar('890904713',XSD_STRING,'nit'),
    'div'               =>  new SoapVar('01',XSD_STRING,'div'),
    'cuenta'            =>  new SoapVar('3',XSD_STRING,'cuenta'),
    'producto'          =>  new SoapVar('23',XSD_STRING,'producto'),
    'origen'            =>  new SoapVar('11001000',XSD_STRING,'origen'),
    'destino'           =>  new SoapVar('05001000',XSD_STRING,'destino'),
    'valoracion'        =>  new SoapVar('50000',XSD_STRING,'valoracion'),
    'nivel_servicio'    =>  new SoapVar($ArrayOfInt,SOAP_ENC_OBJECT,'nivel_servicio'),
    'detalle'           =>  new SoapVar($ArrayOfCotizador_detalleempaques,SOAP_ENC_OBJECT,'detalle'),
    'apikey'            =>  new SoapVar('xxxxxxxxxxxxxxxxxxxxxxxx',XSD_STRING,'apikey'),
    'clave'             =>  new SoapVar('xxxxxxxxxxxxxxxx',XSD_STRING,'clave')
);


$response = $client->Cotizador_cotizar(new SoapVar($Cotizador_cotizarIn,SOAP_ENC_OBJECT,'p'));

PHP Fatal error: Uncaught SoapFault exception: [0] Error, El campo apikey es obligatorio y no puede ser vacio

我这样解决了我的问题。

我为任何 complexType 创建了一个 class,并使用主要名称创建了一个通用 class。


//General class.

<?php  

class General{
    public $p;
}

?>


//Cotizador_cotizarIn class

<?php  

class Cotizador_cotizarIn{

    public $nit;
    public $div;
    public $cuenta;
    public $producto;
    public $origen;
    public $destino;
    public $valoracion;
    public $nivel_servicio;
    public $detalle;
    public $apikey;
    public $clave;

}

?>


//ArrayOfCotizador_detalleempaques class

<?php  

class ArrayOfCotizador_detalleempaques{

    public $item;

}

?>


//ArrayOfInt class

<?php 

class ArrayOfInt {
    public $item;
}

?>



//Cotizador_detalleEmpaques class

<?php  

class Cotizador_detalleEmpaques{

    public $ubl;
    public $alto;
    public $ancho;
    public $largo;
    public $peso;
    public $unidades;


}

?>





//Principal file

<?php

require_once ("classes/cotizador_detalleEmpaques.php");
require_once ("classes/cotizador_cotizarIn.php");
require_once ("classes/arrayOfInt.php");
require_once ("classes/arrayOfCotizador_detalleempaques.php");
require_once ("classes/general.php");

define('APIKEY','XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
define('PASS','XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');

$wsdl = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$client = new SoapClient($wsdl,array('trace' => TRUE,'encoding' => 'UTF-8'));


$params = new General();
$params->p = new Cotizador_cotizarIn();
$params->p->nit = '890904713';
$params->p->div = '01';
$params->p->cuenta = '3';
$params->p->producto = '23';
$params->p->origen = '11001000';
$params->p->destino = '05001000';
$params->p->valoracion = '50000';
$params->p->nivel_servicio = new ArrayOfInt();
$params->p->nivel_servicio->item = 0;
$params->p->detalle = new ArrayOfCotizador_detalleempaques();
$params->p->detalle->item = new Cotizador_detalleEmpaques();
$params->p->detalle->item->ubl  = '0';
$params->p->detalle->item->alto = '12';
$params->p->detalle->item->ancho = '14';
$params->p->detalle->item->largo = '20';
$params->p->detalle->item->peso = '5';
$params->p->detalle->item->unidades = '1';
$params->p->apikey = APIKEY;
$params->p->clave = PASS;

$result = $client->Cotizador_cotizar($params);

print_r(json_encode($result));

?>

我希望这可以帮助其他人。