I need help with this code I’m writing for GNRE in Espirito Santo, using the SOAP standard (version 1.2), I’m programming in PHP (version 7.2.24), and it’s giving this error: SOAP Error: SOAP-ERROR: Parsing WSDL : Couldn’t load from ‘http://schemas.xmlsoap.org/wsdl/’ : failed to load external entity “http://schemas.xmlsoap.org/wsdl/” follows the code:
<?php
function verifyRequiredExtensions() {
$extensions = ['soap', 'openssl', 'xml'];
foreach ($extensions as $ext) {
if (!extension_loaded($ext)) {
die("E01 PHP extension $ext not installed.");
}
}
}
function validateParameters($argc, $argv) {
if ($argc != 5) {
die("E01 Incorrect parameters for execution: php {$argv[0]} <action> <xml_file> <pfx_certificate> <password>");
}
$action = $argv[1];
if (!in_array($action, ['emission', 'pdf'])) {
die("E01 Invalid action: $action");
}
return $action;
}
function validateFiles($xmlFile, $certFile, $caCert) {
if (!file_exists($xmlFile)) {
die("E01 XML file not found: $xmlFile");
}
if (!is_readable($xmlFile)) {
die("E01 XML file not readable: $xmlFile");
}
if (!file_exists($certFile)) {
die("E01 Certificate not found: $certFile");
}
if (!is_readable($certFile)) {
die("E01 Certificate not readable: $certFile");
}
if (!file_exists($caCert)) {
die("E01 CA certificate not found: $caCert");
}
if (!is_readable($caCert)) {
die("E01 CA certificate not readable: $caCert");
}
}
function createSSLContext($caCert) {
return stream_context_create([
'ssl' => [
'verify_peer' => true,
'verify_peer_name' => true,
'allow_self_signed' => false,
'cafile' => $caCert,
'crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT
]
]);
}
function configureSOAPClient($certFile, $certPassword, $sslContext) {
return [
'local_cert' => $certFile,
'passphrase' => $certPassword,
'encoding' => 'UTF-8',
'trace' => true,
'soap_version' => SOAP_1_2,
'stream_context' => $sslContext,
'cache_wsdl' => WSDL_CACHE_NONE,
'connection_timeout' => 60,
'exceptions' => true
];
}
function processSOAPResponse($client, $xmlFile) {
try {
// Reads the content of the XML file
$xmlContent = file_get_contents($xmlFile);
// Prepares the SOAP message body
$params = [
'duaDadosMsg' => $xmlContent
];
// Sends the request
$response = $client->duaObterPdf($params);
// Processes the response
$xml = new SimpleXMLElement($client->__getLastResponse());
$xml->registerXPathNamespace('duae', 'http://www.sefaz.es.gov.br/duae');
// Extracts relevant fields
$return = $xml->xpath('//duae:retObterPdfDua')[0];
$cStat = (string)$return->cStat;
$xMotivo = (string)$return->xMotivo;
if ($cStat != '106') {
die("E01 Unable to obtain PDF of DUA. cStat: $cStat - Reason: $xMotivo");
}
// Extracts and saves the PDF
$pdfBase64 = (string)$return->xPdf;
$pdfContent = base64_decode($pdfBase64);
// Generates the output file name
$outputFile = dirname($xmlFile) . '/' . basename($xmlFile, '.xml') . '.pdf';
if (file_put_contents($outputFile, $pdfContent) === false) {
die("E01 Unable to save PDF file of DUA: $outputFile");
}
echo "PDF of DUA successfully saved in: $outputFile";
} catch (SoapFault $e) {
$message = "E01 SOAP service failure: " . $e->getMessage() . " Last request: " . (isset($client) ? $client->__getLastRequest() : '');
logError($message, $xmlFile);
die("$message");
} catch (Exception $e) {
$message = "E01 General failure: " . $e->getMessage();
logError($message, $xmlFile);
die("$message");
}
}
function logError($message, $xmlFile) {
// Generates the output file name
$logFile = dirname($xmlFile) . '/' . basename($xmlFile, '.xml') . '.log';
$timestamp = date('Y-m-d H:i:s');
file_put_contents($logFile, "[$timestamp] $messagen", FILE_APPEND);
}
// Script Start
verifyRequiredExtensions();
$action = validateParameters($argc, $argv);
$xmlFile = $argv[2];
$certFile = $argv[3];
$certPassword = $argv[4];
// Adjust relative path
if (substr($certFile, 0, 2) == '..') {
$certFile = '/var/www' . substr($certFile, 2);
}
$certFile = str_replace('//', '/', $certFile);
$caCert = '/etc/ssl/certs/ca-certificates.crt';
validateFiles($xmlFile, $certFile, $caCert);
$sslContext = createSSLContext($caCert);
$options = configureSOAPClient($certFile, $certPassword, $sslContext);
try {
// Define WSDL
$wsdl = 'https://app.sefaz.es.gov.br/WsDua/DuaService.asmx?wsdl';
// Create SOAP client
$client = new SoapClient($wsdl, $options);
// Prepare SOAP header
$header = new SoapHeader(
'http://www.sefaz.es.gov.br/duae',
'duaServiceHeader',
['versao' => '1.01']
);
$client->__setSoapHeaders($header);
processSOAPResponse($client, $xmlFile);
} catch (Exception $e) {
$message = "E01 Failed to configure SOAP client: " . $e->getMessage();
logError($message, $xmlFile);
die("$message");
}
?>