如何使用soapclient创建此请求?

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

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

enter image description here

出于安全原因,我无法发布一些真正的值(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致命错误:未捕获SoapFault异常:[0]错误,apikey字段是必填字段且不能为空

php soap-client
1个回答
0
投票

我解决了这个问题。

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


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

?>

我希望这可以帮助别人。

© www.soinside.com 2019 - 2024. All rights reserved.