It is extrange that a default class method fails that way, sometimes, even when the arguments passed to the method are the same that usualy takes a fraction of second.
Maximum execution time of 30 seconds exceeded in script_path on line script_line_number
In that exact line:
$result = $DOMDocument -> schemaValidate($schemaPath);
$DOMDocument is always the same. And it only references parts of the same XML with ID atrtibutes. It does not have any URL like attribute, besides Algorith and xmlns ones, which by nature does not call anyresource from anywhere, and we are talking about DOMDocument class of PHP and the XML starndards.
$schemaPath
is always the same, and it is pointing to a server local XSD file, which is always there, before, and after the validation attempt, either it is successful or not. The schema is only pointing to other local xsd files, located in the same folder i.e. <xs:include schemaLocation="schema2.xsd"/>
The only possible answer I can think of, is that the XSD file is located by the method but for some reason it cannot be read, because the disc is busy.
What could be causing the execution of the method take so long?
What measures should be taken to prevent the error to happen besides increasing the maximum execution time limit of PHP?
The XML and the XSD files are pretty small, in fact the exact same XML and XSD usually takes less than ~ 0.1 seconds to validate, but a very few times (~ 1 out of 1000) the execution time exeeds 30 seconds.
EDIT
I isolated the problem so I post a samples.
Schema.xsd:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema targetNamespace="http://www.foo.bar/Car" xmlns:SiiDte="http://www.foo.bar/Car" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:include schemaLocation="schema2.xsd"/>
<xs:import namespace="http://www.w3.org/2000/09/xmldsig#" schemaLocation="xmldsignature_v10.xsd"/><!-- just the standar signature schema -->
<xs:element name="ROOT" type="SiiDte:ROOTDefType"/>
<xs:complexType name="ROOTDefType">
<xs:sequence>
<xs:element name="Element"></xs:element>
<xs:element ref="ds:Signature">
<xs:annotation>
<xs:documentation>Firma Digital sobre Documento</xs:documentation>
</xs:annotation>
</xs:element>
</xs:sequence>
<xs:attribute name="version" type="xs:decimal" use="required" fixed="1.0"/>
</xs:complexType>
</xs:schema>
Schema2.xsd:
<xs:schema targetNamespace="http://www.foo.bar/Car" xmlns:ns1="http://www.foo.bar/Car" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:simpleType name="MOOType">
<xs:restriction base="xs:positiveInteger">
<xs:enumeration value="1"/>
<xs:enumeration value="2"/>
<xs:enumeration value="3"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
Code:
// ... a bunch of ther code...
$XML =
'<?xml version="1.0"?>
<ROOT xmlns="http://www.foo.bar/Car" version="1.0">
<Element ID="A1">hello</Element>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
<CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
<SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
<Reference URI="#A1">
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
<DigestValue>base64string</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>base64string</SignatureValue>
<KeyInfo>
<KeyValue>
<RSAKeyValue>
<Modulus>base64string</Modulus>
<Exponent>AQAB</Exponent>
</RSAKeyValue>
</KeyValue>
<X509Data>
<X509Certificate>base64string</X509Certificate>
</X509Data>
</KeyInfo>
</Signature>
</ROOT>'
;
$DD = new DOMDocument();
$DD -> loadXML($XML);
$i = 0;
while ($i < 100) {
// ... a bunch of other code...
libxml_use_internal_errors(true);
$old_libxml_disable_entity_loader = libxml_disable_entity_loader(false); $result = $DD -> schemaValidate(__DIR__ . '/schema.xsd');
libxml_disable_entity_loader($old_libxml_disable_entity_loader); // Se desactiva nuevamente carga de entidades para descartar entidades maliciosas
$i++;
echo str_pad($i, 5) . ($result ? 'true' : 'false') . '<br>';
// ... a bunch of other code...
}