14

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)
Nik
  • 2,004
  • 2
  • 16
  • 23
AmrataB
  • 897
  • 2
  • 8
  • 17
  • 2
    Check for namespace, you might need to call with `\SimpleXMLElement` – anwerj Oct 06 '17 at 16:56
  • Its somewhere in AWS library...nowhere in my code. – AmrataB Oct 06 '17 at 17:02
  • 1
    May be this can help. https://stackoverflow.com/a/35834836/3305978 – anwerj Oct 06 '17 at 17:10
  • I attached the same link in my question. As I have shown in the extensions list, xml and simplexml is already present but when I run extensions_loaded('php-xml'), it returns false. Do I have to install php7.0-xml or simplexml separately? – AmrataB Oct 06 '17 at 17:24

6 Answers6

18

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.

nandal
  • 2,206
  • 1
  • 11
  • 19
11

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

max johnson
  • 133
  • 1
  • 9
5

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.

AmrataB
  • 897
  • 2
  • 8
  • 17
4

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);
Piyush Sharma
  • 381
  • 2
  • 8
2

Try

use SimpleXMLElement;

before calling the class.

Sameer Ali
  • 88
  • 7
  • In my file? I am not using this class directly and in the library where they are using it they are actually using it correctly. But anyways, I tried it and it did not work. – AmrataB Oct 06 '17 at 17:37
  • This is helpful if you use this class in another class (OOP approach) – zanvidmar Jan 16 '20 at 17:24
2

I solved running this command

sudo systemctl restart php-fpm
Pablo Salazar
  • 401
  • 4
  • 10