Connect with the web service of SII AEAT (soap PHP)

3

First of all, a greeting to all those interested in this question. I begin with the explanation of the problem:

After trying to establish the connection with the web service of AEAT for the management of the xml files I have found a good stone on the way. The code I am using to make the connection is:

        $ruta       = FCPATH.'temp/uploads/'.$_SERVER['SERVER_NAME'].'/Certificado.pfx';
        $handle     = fopen($ruta, "r");
        $contents   = fread($handle, filesize($ruta));
        fclose($handle);

        $url = 'https://www7.aeat.es/wlpl/SSII-FACT/ws/fe/SiiFactFEV1SOAP.wsdl';

        $params = array( 
            "location"      => $url,
            "uri"           => '',
            "local_cert"    => $contents,
            "passphrase"    => xxxxxxxx, // clave del certificado
            "style"         => SOAP_DOCUMENT,
            "authentication"=> SOAP_AUTHENTICATION_BASIC,
            "use"           => SOAP_LITERAL, 
            "encoding"      => 'utf-8', 
            "soap_version"  => SOAP_1_1, 
            "trace"         => 1, 
            "exceptions"    => 0, 
            "connection_timeout" => 1000 
        ); 

        $soapclient = new SoapClient(null, $params);
        var_dump($soapclient->test());

When creating the object I managed to connect, but the var_dump returns a soapFault with the following error message:

  

protected 'message' = > string 'looks like we got no XML document' (length = 33)

The soapClient object has been created correctly and connects to the host authenticating with the certificate , at this point I have been blocked. I can not add the xml without the web service giving me an error stating that there is no certificate information or it is not correct and that it is necessary to have it installed, I can not access the server-side functions. ..

On the other hand I have not managed to create even a soapClient object if I use the constructor in WSDL mode, nor its error classes (soapFault).

To make the document submission tests (you must have a valid certificate installed on the computer):   link

If someone wants to check the web service directly, the url is:

https://www7.aeat.es/wlpl/SSII-FACT/ws/fe/SiiFactFEV1SOAP

Hacienda has answered this to the question of whether it is necessary to add the certificate in the soapClient object:

  

YES. The presentation may be made by the taxpayer, a   empowered by this procedure or a social partner, who must   have a recognized electronic certificate. Therefore, the use   of services requires having an electronic certificate installed   admitted by the Tax Agency, on the computer from which the   produce the sending of information. Due to the volume of questions not   We can give support on how to configure or manage any of the   Multiple existing client applications to connect to a Web   Service. Sincerely, AEAT

It should be pointed out that:

  

The certificate that I have installed and running   correctly in the equipment is the same that I command in the object    soapClient and it returns an error.

I would appreciate any help that could at least guide me to the solution of sending documents with this web service.

Thanks for your time!

    
asked by Gonsabb 09.06.2017 в 12:54
source

3 answers

1

I would like to add a note in case other people try to implement the SII from PHP.

You do not really need to create classes / objects for the data structure.

That is, simply create a PHP multi-level associative array with the entire structure and pass it as a parameter to the function that we call, and that is enough, then the same SOAP client is responsible for coding the data with the necessary XML structure.

Example of the PHP array with var_export ():

$datos=array (
  'Cabecera' => 
  array (
    'IDVersionSii' => '1.0',
    'Titular' => 
    array (
      'NombreRazon' => 'ALEXANDRU CATALIN TRANDAFIR',
      'NIF' => 'X4378072E',
    ),
    'TipoComunicacion' => 'A0',
  ),
  'RegistroLRFacturasEmitidas' => 
  array (
    0 => 
    array (
      'PeriodoImpositivo' => 
      array (
        'Ejercicio' => '2017',
        'Periodo' => '12',
      ),
      'IDFactura' => 
      array (
        'IDEmisorFactura' => 
        array (
          'NIF' => 'X4378072E',
        ),
        'NumSerieFacturaEmisor' => 'SIIDEMO/1',
        'FechaExpedicionFacturaEmisor' => '31-12-2017',
      ),
      'FacturaExpedida' => 
      array (
        'TipoFactura' => 'F1',
        'ClaveRegimenEspecialOTrascendencia' => '01',
        'ImporteTotal' => 1210,
        'DescripcionOperacion' => 'Compra ordenador portátil',
        'FechaOperacion' => '31-12-2017',
        'Contraparte' => 
        array (
          'NombreRazon' => 'HeavyDots SL',
          'NIF' => 'B66544586',
        ),
        'TipoDesglose' => 
        array (
          'DesgloseFactura' => 
          array (
            'Sujeta' => 
            array (
              'NoExenta' => 
              array (
                'TipoNoExenta' => 'S1',
                'DesgloseIVA' => 
                array (
                  'DetalleIVA' => 
                  array (
                    0 => 
                    array (
                      'TipoImpositivo' => 21,
                      'BaseImponible' => 500,
                      'CuotaRepercutida' => 105,
                    ),
                    1 => 
                    array (
                      'TipoImpositivo' => 21,
                      'BaseImponible' => 500,
                      'CuotaRepercutida' => 105,
                    ),
                  ),
                ),
              ),
            ),
          ),
        ),
      ),
    ),
    1 => 
    array (
      'PeriodoImpositivo' => 
      array (
        'Ejercicio' => '2017',
        'Periodo' => '12',
      ),
      'IDFactura' => 
      array (
        'IDEmisorFactura' => 
        array (
          'NIF' => 'X4378072E',
        ),
        'NumSerieFacturaEmisor' => 'SIIDEMO/2',
        'FechaExpedicionFacturaEmisor' => '31-12-2017',
      ),
      'FacturaExpedida' => 
      array (
        'TipoFactura' => 'F1',
        'ClaveRegimenEspecialOTrascendencia' => '01',
        'ImporteTotal' => 1210,
        'DescripcionOperacion' => 'Compra ordenador portátil',
        'FechaOperacion' => '31-12-2017',
        'Contraparte' => 
        array (
          'NombreRazon' => 'HeavyDots SL',
          'NIF' => 'B66544586',
        ),
        'TipoDesglose' => 
        array (
          'DesgloseFactura' => 
          array (
            'Sujeta' => 
            array (
              'NoExenta' => 
              array (
                'TipoNoExenta' => 'S1',
                'DesgloseIVA' => 
                array (
                  'DetalleIVA' => 
                  array (
                    0 => 
                    array (
                      'TipoImpositivo' => 21,
                      'BaseImponible' => 500,
                      'CuotaRepercutida' => 105,
                    ),
                    1 => 
                    array (
                      'TipoImpositivo' => 21,
                      'BaseImponible' => 500,
                      'CuotaRepercutida' => 105,
                    ),
                  ),
                ),
              ),
            ),
          ),
        ),
      ),
    ),
  ),
)

Then the call:

$comando = 'SuministroLRFacturasEmitidas';
$response = $client->$comando($datos);

And then if we want we can get the generated XML like this:

$peticion_xml = $client->__getLastRequest();

Generated XML:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="https://www2.agenciatributaria.gob.es/static_files/common/internet/dep/aplicaciones/es/aeat/ssii/fact/ws/SuministroInformacion.xsd" xmlns:ns2="https://www2.agenciatributaria.gob.es/static_files/common/internet/dep/aplicaciones/es/aeat/ssii/fact/ws/SuministroLR.xsd">
  <SOAP-ENV:Body>
    <ns2:SuministroLRFacturasEmitidas>
      <ns1:Cabecera>
        <ns1:IDVersionSii>1.0</ns1:IDVersionSii>
        <ns1:Titular>
          <ns1:NombreRazon>ALEXANDRU CATALIN TRANDAFIR</ns1:NombreRazon>
          <ns1:NIF>X4378072E</ns1:NIF>
        </ns1:Titular>
        <ns1:TipoComunicacion>A0</ns1:TipoComunicacion>
      </ns1:Cabecera>
      <ns2:RegistroLRFacturasEmitidas>
        <ns1:PeriodoImpositivo>
          <ns1:Ejercicio>2017</ns1:Ejercicio>
          <ns1:Periodo>12</ns1:Periodo>
        </ns1:PeriodoImpositivo>
        <ns2:IDFactura>
          <ns1:IDEmisorFactura>
            <ns1:NIF>X4378072E</ns1:NIF>
          </ns1:IDEmisorFactura>
          <ns1:NumSerieFacturaEmisor>SIIDEMO/1</ns1:NumSerieFacturaEmisor>
          <ns1:FechaExpedicionFacturaEmisor>31-12-2017</ns1:FechaExpedicionFacturaEmisor>
        </ns2:IDFactura>
        <ns2:FacturaExpedida>
          <ns1:TipoFactura>F1</ns1:TipoFactura>
          <ns1:FechaOperacion>31-12-2017</ns1:FechaOperacion>
          <ns1:ClaveRegimenEspecialOTrascendencia>01</ns1:ClaveRegimenEspecialOTrascendencia>
          <ns1:ImporteTotal>1210</ns1:ImporteTotal>
          <ns1:DescripcionOperacion>Compra ordenador portátil</ns1:DescripcionOperacion>
          <ns1:Contraparte>
            <ns1:NombreRazon>HeavyDots SL</ns1:NombreRazon>
            <ns1:NIF>B66544586</ns1:NIF>
          </ns1:Contraparte>
          <ns1:TipoDesglose>
            <ns1:DesgloseFactura>
              <ns1:Sujeta>
                <ns1:NoExenta>
                  <ns1:TipoNoExenta>S1</ns1:TipoNoExenta>
                  <ns1:DesgloseIVA>
                    <ns1:DetalleIVA>
                      <ns1:TipoImpositivo>21</ns1:TipoImpositivo>
                      <ns1:BaseImponible>500</ns1:BaseImponible>
                      <ns1:CuotaRepercutida>105</ns1:CuotaRepercutida>
                    </ns1:DetalleIVA>
                    <ns1:DetalleIVA>
                      <ns1:TipoImpositivo>21</ns1:TipoImpositivo>
                      <ns1:BaseImponible>500</ns1:BaseImponible>
                      <ns1:CuotaRepercutida>105</ns1:CuotaRepercutida>
                    </ns1:DetalleIVA>
                  </ns1:DesgloseIVA>
                </ns1:NoExenta>
              </ns1:Sujeta>
            </ns1:DesgloseFactura>
          </ns1:TipoDesglose>
        </ns2:FacturaExpedida>
      </ns2:RegistroLRFacturasEmitidas>
      <ns2:RegistroLRFacturasEmitidas>
        <ns1:PeriodoImpositivo>
          <ns1:Ejercicio>2017</ns1:Ejercicio>
          <ns1:Periodo>12</ns1:Periodo>
        </ns1:PeriodoImpositivo>
        <ns2:IDFactura>
          <ns1:IDEmisorFactura>
            <ns1:NIF>X4378072E</ns1:NIF>
          </ns1:IDEmisorFactura>
          <ns1:NumSerieFacturaEmisor>SIIDEMO/2</ns1:NumSerieFacturaEmisor>
          <ns1:FechaExpedicionFacturaEmisor>31-12-2017</ns1:FechaExpedicionFacturaEmisor>
        </ns2:IDFactura>
        <ns2:FacturaExpedida>
          <ns1:TipoFactura>F1</ns1:TipoFactura>
          <ns1:FechaOperacion>31-12-2017</ns1:FechaOperacion>
          <ns1:ClaveRegimenEspecialOTrascendencia>01</ns1:ClaveRegimenEspecialOTrascendencia>
          <ns1:ImporteTotal>1210</ns1:ImporteTotal>
          <ns1:DescripcionOperacion>Compra ordenador portátil</ns1:DescripcionOperacion>
          <ns1:Contraparte>
            <ns1:NombreRazon>HeavyDots SL</ns1:NombreRazon>
            <ns1:NIF>B66544586</ns1:NIF>
          </ns1:Contraparte>
          <ns1:TipoDesglose>
            <ns1:DesgloseFactura>
              <ns1:Sujeta>
                <ns1:NoExenta>
                  <ns1:TipoNoExenta>S1</ns1:TipoNoExenta>
                  <ns1:DesgloseIVA>
                    <ns1:DetalleIVA>
                      <ns1:TipoImpositivo>21</ns1:TipoImpositivo>
                      <ns1:BaseImponible>500</ns1:BaseImponible>
                      <ns1:CuotaRepercutida>105</ns1:CuotaRepercutida>
                    </ns1:DetalleIVA>
                    <ns1:DetalleIVA>
                      <ns1:TipoImpositivo>21</ns1:TipoImpositivo>
                      <ns1:BaseImponible>500</ns1:BaseImponible>
                      <ns1:CuotaRepercutida>105</ns1:CuotaRepercutida>
                    </ns1:DetalleIVA>
                  </ns1:DesgloseIVA>
                </ns1:NoExenta>
              </ns1:Sujeta>
            </ns1:DesgloseFactura>
          </ns1:TipoDesglose>
        </ns2:FacturaExpedida>
      </ns2:RegistroLRFacturasEmitidas>
    </ns2:SuministroLRFacturasEmitidas>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

For those who are still interested in integrating SII in PHP applications (and not only), I would like to tell you that I will soon finish writing and publishing the ebook / tutorial at link

    
answered by 09.01.2018 / 13:13
source
1

Sorry for the delay, I already have a connection to the webservice and the functions, but I still have not gotten a correct response from the webservice.

To connect via soap via php:

$url = 'https://www7.aeat.es/wlpl/SSII-FACT/ws/fe/SiiFactFEV1SOAP';
$params1 = array( 
                "location"      => $url,
                "style"         => SOAP_DOCUMENT,
                "use"           => SOAP_LITERAL, 
                "trace"         => 1, 
                "exceptions"    => 0,
                "cache_wsdl"    => 0
            ); 
$soapclient = new SoapClient('https://www2.agenciatributaria.gob.es/static_files/common/internet/dep/aplicaciones/es/aeat/ssii/fact/ws/SuministroFactEmitidas.wsdl', $params1);

//obtener las funciones a las que puedo llamar
 $funciones = $soapclient->__getFunctions();
 echo "<h2>Funciones del servicio</h2>";
 foreach ($funciones as $funcion)
 echo $funcion . "<br />";

//obtener los tipos de datos involucrados
 echo "<h2>Tipos en el servicio</h2>";
 $tipos = $soapclient->__getTypes();
 foreach ($tipos as $tipo)
 echo $tipo . "<br /><br />";

As you can see I am connecting via WSDL to the url: link

From here you can find the outline of the functions that can be called and the data structures they need.

From this url you can access all the wsdl of the hacienda webservice:

  

answered by 16.06.2017 в 11:26
0

You can use this website to generate your PHP classes

link

Then you can write your code like this:

$client = new SoapClient("https://www2.agenciatributaria.gob.es/static_files/common/internet/dep/aplicaciones/es/aeat/ssii/fact/ws/SuministroFactEmitidas.wsdl");

/* Usa el nombre de la clases generada por la pagina wsdltophp */
$detalleIVA = new DetalleIVAEmitidaType (TipoImpositivo, BaseImponible...);

/* Set your parameters for the request */
$params = array(
  "DetalleIVA" => $detalleIVA,
  "dato2" => "Prueba",
  "dato3" => 111,
...
);

$response = $client->__soapCall("SuministroLRFacturasEmitidas", array($params));

/* Aqui puedes probar la respuesta */
var_dump($response);
    
answered by 16.06.2017 в 23:35