Skip to content

Instantly share code, notes, and snippets.

@jkfran
Created May 3, 2017 10:08
Show Gist options
  • Save jkfran/39248c5975a19fe060e6aa570eeb5a7e to your computer and use it in GitHub Desktop.
Save jkfran/39248c5975a19fe060e6aa570eeb5a7e to your computer and use it in GitHub Desktop.
Comprobar validez de NIF y Nombre con la Agencia Tributaria
<?php
/**
* Previamente convertir ceritificado .p12 o pfx a .key.pem y .crt.pem:
*
* openssl pkcs12 -in ciar.pfx -out cia.key.pem -nocerts -nodes
* openssl pkcs12 -in ciar.p12 -out ciar.crt.pem -clcerts -nokeys
*
**/
$CERT_CRT_PEM = 'certificado.crt.pem';
$CERT_CRT_PASS = '123123';
$CERT_KEY_PEM = 'certificado.key.pem';
$CERT_KEY_PASS = '123123';
$URL = 'https://www1.agenciatributaria.gob.es/wlpl/BURT-JDIT/ws/VNifV1SOAP';
// Get the SOAP data into a string, I am using HEREDOC syntax
// but how you do this is irrelevant, the point is just get the
// body of the request into a string
$mySOAP = <<<EOD
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:vnif="http://www2.agenciatributaria.gob.es/static_files/common/internet/dep/aplicaciones/es/aeat/burt/jdit/ws/VNifV1Ent.xsd" xmlns:LocalPart="http://www2.agenciatributaria.gob.es/static_files/common/internet/dep/aplicaciones/es/aeat/burt/jdit/ws/VNifV1Ent.xsd">
<soapenv:Header/>
<soapenv:Body>
<vnif:VNifV1Ent>
<vnif:Nif>123456789A</vnif:Nif>
<vnif:Nombre>Apellidos Nombre</vnif:Nombre>
</vnif:VNifV1Ent>
</soapenv:Body>
</soapenv:Envelope>
EOD;
// The HTTP headers for the request
$headers = array(
'Content-Type: text/xml; charset=utf-8',
'Content-Length: '.strlen($mySOAP),
);
// Build the cURL session
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $mySOAP);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSLCERT, $CERT_CRT_PEM);
curl_setopt($ch, CURLOPT_SSLKEY, $CERT_KEY_PEM);
curl_setopt($ch, CURLOPT_SSLCERTPASSWD, $CERT_CRT_PASS);
curl_setopt($ch, CURLOPT_SSLKEYPASSWD, $CERT_KEY_PASS);
// Send the request and check the response
if (($result = curl_exec($ch)) === FALSE) {
die('CURL error: '.curl_error($ch)."<br />\n");
} else {
echo "Success!\n";
}
curl_close($ch);
// Handle the response from a successful request
$xmlobj = simplexml_load_string($result);
$data = $xmlobj->children('env', true)->children('VNifV1Sal', true)->VNifV1Sal;
echo $data->Nif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment