Class 'SimpleXMLElement' not found

Viewed 38524

I am trying to use digital ocean spaces in my php7 laravel project. The code is pretty simple to just load a file and copy it to the directory. Everything works on my local machine but fails on my digital ocean server with this error

Class 'SimpleXMLElement' not found

Even on digital ocean server, if I use tinker to run the same storage commands, it works fine. What can be the issue?

My get_loaded_extensions() returns get_loaded_extensions()

[
     "Core",
     "date",
     "libxml",
     "openssl",
     "pcre",
     "zlib",
     "filter",
     "hash",
     "pcntl",
     "Reflection",
     "SPL",
     "session",
     "standard",
     "mysqlnd",
     "PDO",
     "xml",
     "apcu",
     "calendar",
     "ctype",
     "curl",
     "dom",
     "mbstring",
     "fileinfo",
     "ftp",
     "gd",
     "gettext",
     "iconv",
     "json",
     "exif",
     "mysqli",
     "pdo_mysql",
     "Phar",
     "posix",
     "readline",
     "shmop",
     "SimpleXML",
     "sockets",
     "sysvmsg",
     "sysvsem",
     "sysvshm",
     "tokenizer",
     "wddx",
     "xmlreader",
     "xmlrpc",
     "xmlwriter",
     "xsl",
     "zip",
     "Zend OPcache",
   ]

so it looks like xml and simplexml are installed. As suggested by the link, php-xml and php-simplexml should be installed. Do I need to install something separately?

Also, my file upload code looks like this

public function uploadNew($file, $title, User $user)
    {
        if (!File::directoryExists($user->username)) {
            Storage::makeDirectory($user->username);
        }
        $completeFileName = $user->username.'/'.$title.time();
        Storage::put($completeFileName, file_get_contents($file), 'public');
        $this->url = File::baseUrl().$completeFileName;
        $this->title = $title;
        $this->user_id = $user->id;
        $this->save();
        return $this;
    }

    public static function baseUrl()
    {
        return 'https://'.env('DO_SPACES_BUCKET').'.nyc3.digitaloceanspaces.com/';
    }

    public static function directoryExists($directory)
    {
        $existingDirs = Storage::directories();
        return in_array($directory, $existingDirs);
    }

Adding the stack trace of exception:

(1/1) FatalThrowableError
Class 'SimpleXMLElement' not found
in PayloadParserTrait.php (line 39)
at RestXmlParser->parseXml(object(Stream))
in RestXmlParser.php (line 33)
at RestXmlParser->payload(object(Response), object(StructureShape), array())
in AbstractRestParser.php (line 62)
at AbstractRestParser->__invoke(object(Command), object(Response))
in RetryableMalformedResponseParser.php (line 37)
at RetryableMalformedResponseParser->__invoke(object(Command), object(Response))
in AmbiguousSuccessParser.php (line 58)
at AmbiguousSuccessParser->__invoke(object(Command), object(Response))
in GetBucketLocationParser.php (line 30)
at GetBucketLocationParser->__invoke(object(Command), object(Response))
in WrappedHttpHandler.php (line 126)
at WrappedHttpHandler->parseResponse(object(Command), object(Request), object(Response), array())
in WrappedHttpHandler.php (line 93)
at WrappedHttpHandler->Aws\{closure}(object(Response))
in Promise.php (line 203)
8 Answers

check the version of installed php using:-

$ php -v

If it's version 7, then install php7.0-xml

if it's version 7.2.*, then use php7.2-xml package on your machine along with php.

Just install php-xml package.For reference, just install all the additional php libraries:

apt-get install php-pear php7.2-curl php7.2-dev php7.2-gd php7.2-mbstring php7.2-zip php7.2-mysql php7.2-xml

Finally, I just had to follow the same answer I have mentioned in question. installed php7.0-xml and simplexml had to be installed and apache server restarted to get it to work. So, even if your loaded extensions show xml and simplexml as installed, you might still need to install the php7.0-xml and simplexml.

You have to put \ slash in front of SimpleXMLElement (in laravel) in order to call xml element class

$url = "https://abc.xyz.com";
$response = file_get_contents($url);
$xmlDoc = new \SimpleXMLElement($response);

I solved running this command

sudo systemctl restart php-fpm

To fix this, install the latest version of php-xml, run this:

sudo apt-get install php-xml -y

Don't forget to restart apache:

sudo systemctl restart apache2

Try

use SimpleXMLElement;

before calling the class.

I just had to restart apache2 after installing

systemctl apache2 restart

Related