Search code examples
phpapachelibxml2

libxml_set_external_entity_loader doesn't work in httpd


I can't get the function libxml_set_external_entity_loader work within apache.

This is my test.php (took from http://php.net/manual/en/function.libxml-set-external-entity-loader.php):

<?php
$xml = <<<XML
<!DOCTYPE foo PUBLIC "-//FOO/BAR" "http://example.com/foobar">
<foo>bar</foo>
XML;

$dtd = <<<DTD
<!ELEMENT foo (#PCDATA)>
DTD;

libxml_disable_entity_loader(false);

libxml_set_external_entity_loader(
    function ($public, $system, $context) use($dtd) {
        var_dump($public);
        var_dump($system);
        var_dump($context);
        $f = fopen("php://temp", "r+");
        fwrite($f, $dtd);
        rewind($f);
        return $f;
    }
);

$dd = new DOMDocument;
$r  = $dd->loadXML($xml);

var_dump($dd->validate());

The result always failed in browser when access this test.php file:

Warning: DOMDocument::validate(http://example.com/foobar): failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found in /www/test.php on line 28

Warning: DOMDocument::validate(): I/O warning : failed to load external entity "http://example.com/foobar" in /www/test.php on line 28

Warning: DOMDocument::validate(): Could not load the external subset "http://example.com/foobar" in /www/test.php on line 28
bool(false) 

While it works with php in terminal:

$ php test.php


string(10) "-//FOO/BAR"
string(25) "http://example.com/foobar"
array(4) {
  ["directory"]=>
  NULL
  ["intSubName"]=>
  NULL
  ["extSubURI"]=>
  NULL
  ["extSubSystem"]=>
  NULL
}
bool(true)

Both httpd and php were built from source on Mac OS. phpinfo() result is uploaded here https://dl.dropboxusercontent.com/u/11335326/phpinfo().html

Can anyone point out which is the problem? Thanks!


Solution

  • Rebuid httpd with --enable-mpms-shared=all and then add LoadModule mpm_prefork_module modules/mod_mpm_prefork.so into httpd.conf solved this.