Search code examples
phpsoap-client

How to create this request with soapclient?


I need to create a request from WS SOAP, but I try to set params and always return me a mistake, someone can help me and show me how I can build this request.

enter image description here

for security reasons, I can not post some real values (WSDL,apikey, pass), but I test this WS with soapUI with the real values and the result was ok.

I did these options:

TEST 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'));

TEST 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


Solution

  • I solved my problem like this.

    I created a class for any complexType and a general class with the principal names.

    
    //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));
    
    ?>
    

    I hope this can help someone else.