PHP - INVOCAR web services con WSECURITY

Filtrado por el usuario: Pedro Santana
<<>>
Filtrado por el usuario 'Pedro Santana' (Eliminar fitro)
 
Vista:

INVOCAR web services con WSECURITY

Publicado por Vladimir zorrilla llerena (1 intervención) el 26/09/2022 19:07:55
Buenos dias

Al invocar un servicio web me sale este error

El resultado es SoapFault exception: [ns0:Server] javax.xml.ws.soap.SOAPFaultException in /home/nginx/domains/devconta.aduasoft.com/public/todocosmos.php:98 Stack trace: #0 /home/nginx/domains/devconta.aduasoft.com/public/todocosmos.php(98): SoapClient->__soapCall('realizarConsult...', Array) #1 /home/nginx/domains/devconta.aduasoft.com/public/todocosmos.php(134): Seida->realizarConsulta('') #2 {main}object(SoapFault)#11 (9) { ["message":protected]=> string(36) "javax.xml.ws.soap.SOAPFaultException" ["string":"Exception":private]=> string(0) "" ["code":protected]=> int(0) ["file":protected]=> string(63) "/home/nginx/domains/devconta.aduasoft.com/public/todocosmos.php" ["line":protected]=> int(98) ["trace":"Exception":private]=> array(2) { [0]=> array(6) { ["file"]=> string(63) "/home/nginx/domains/devconta.aduasoft.com/public/todocosmos.php" ["line"]=> int(98) ["function"]=> string(10) "__soapCall" ["class"]=> string(10) "SoapClient" ["type"]=> string(2) "->" ["args"]=> array(2) { [0]=> string(16) "realizarConsulta" [1]=> array(1) { ["parameters"]=> string(125) "190222262022" } } } [1]=> array(6) { ["file"]=> string(63) "/home/nginx/domains/devconta.aduasoft.com/public/todocosmos.php" ["line"]=> int(134) ["function"]=> string(16) "realizarConsulta" ["class"]=> string(5) "Seida" ["type"]=> string(2) "->" ["args"]=> array(1) { [0]=> string(0) "" } } } ["previous":"Exception":private]=> NULL ["faultstring"]=> string(36) "javax.xml.ws.soap.SOAPFaultException" ["faultcode"]=> string(10) "ns0:Server" }


AQUI ESTA EL CODIGO COMPLETO

<?php
class CustomHeaders extends SoapHeader {
private $wss_ns = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd';
function __construct($user, $pass, $ns = null) {
if ($ns) {
$this->wss_ns = $ns;
}
$auth = new stdClass();
$auth->Username = new SoapVar($user, XSD_STRING, NULL, $this->wss_ns, NULL, $this->wss_ns);
$auth->Password = new SoapVar($pass, XSD_STRING, NULL, $this->wss_ns, NULL, $this->wss_ns);
$username_token = new stdClass();
$username_token->UsernameToken = new SoapVar($auth, SOAP_ENC_OBJECT, NULL, $this->wss_ns, 'UsernameToken', $this->wss_ns);
$security_sv = new SoapVar( new SoapVar($username_token, SOAP_ENC_OBJECT, NULL, $this->wss_ns, 'UsernameToken', $this->wss_ns), SOAP_ENC_OBJECT, NULL, $this->wss_ns, 'Security', $this->wss_ns);
parent::__construct($this->wss_ns, 'Security', $security_sv, true);
}
}
/**
* Seida SUNAT
*/
class Seida
{

protected $ruc;
protected $usuario_sol;
protected $clave_sol;
protected $soapHeader;
protected $soapClient;
protected $webService = 'https://e-seida.sunat.gob.pe/ol-ad-itseida-ws/ReceptorService.htm?wsdl';

/**
* @param $ruc
* @param $usuario_sol
* @param $clave_sol
*/
function __construct(String $ruc, String $usuario_sol, String $clave_sol){
// CABECERA DEL WEB SERVICE
$headers = new CustomHeaders($usuario_sol,$clave_sol);

$this->ruc = $ruc;
$this->usuario_sol = $usuario_sol;
$this->clave_sol = $clave_sol;

$context = stream_context_create([
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
]
]);

$this->soapClient = new SoapClient($this->webService,['stream_context' => $context, 'exceptions' => 0]);
$this->soapClient->soap_defencoding = 'UTF-8';
//echo $this->getUser();
//$this->soapHeader = WsSecurity::createWsSecuritySoapHeader($this->getUser(), $this->clave_sol, false);
$this->soapHeader = $headers;
}

/**
* Recibe la ruta del archivo para subirlo
* @param String $numero_transaccion
* @param String $ruta
* @return boolean
*/
public function recibirArchivo(String $numero_transaccion, String $file)
{

try {

$this->soapClient->__setSoapHeaders($this->soapHeader);

//preparo archivo , lo codifico en base64
$fileString = base64_encode(fread(fopen($file, "r"), filesize($file)));
$result = $this->soapClient->__soapCall('recibirArchivo', ['numeroTransaccion' => $numero_transaccion ,'informacionArchivo' => $fileString]);

return $result;

} catch (Exception $e) {
error_log("recibirArchivo Exception: " . $e->getMessage());
}

return false;
}

/**
*
* @param String $parametros_consulta
* @param String $ruta
* @return ??
*/
public function realizarConsulta(String $parametros_consulta)
{

try {
//$consulta = "<consulta><tipo>1</tipo><parametros><numeroTicket>540D3D995F35</numeroTicket><annoTicket>17631</annoTicket></parametros></consulta>";
$consulta = "<consulta><tipo>1</tipo><parametros><numeroTicket>9022226</numeroTicket><annoTicket>2022</annoTicket></parametros></consulta>";

$this->soapClient->__setSoapHeaders($this->soapHeader);
$result = $this->soapClient->__soapCall("realizarConsulta",['parameters'=>$consulta]);
echo "El resultado es ".$result;
return $result;

} catch (Exception $e) {
error_log("realizarConsulta Exception: " . $e->getMessage());
}

return false;
}

/**
* Obtener funciones
* @return null
*/
public function test()
{
//$this->soapClient->__setSoapHeaders($this->soapHeader);
var_dump($this->soapClient->__getFunctions());
}

public function getUser()
{
$macAddress = MacAddress::generateMacAddress(); //MacAddress::getCurrentMacAddress('enp1s0');//
$ipAddress = '192.168.1.26';//ClientIP::get();

$user = [$this->ruc.$this->usuario_sol, $macAddress, $ipAddress, 2];

return implode('|', $user);
}

}

$seida = new Seida('20100010136','PILOTONS', 'moddatos');
//$seida->test();
//echo var_dump($seida->getUser());
$rs = $seida->realizarConsulta('');
echo var_dump($rs);
/*$rs = $seida->recibirArchivo('0101','0101.zip');
echo var_dump($rs);
$fileString = base64_encode(fread(fopen('0101.zip', "r"), filesize('0101.zip')));
echo $fileString;*/
//enp1s0
?>
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder