laravel 找不到类“SimpleXMLElement”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46610501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:45:32  来源:igfitidea点击:

Class 'SimpleXMLElement' not found

phplaravelamazon-s3

提问by AmrataB

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

我正在尝试在我的 php7 laravel 项目中使用数字海洋空间。代码非常简单,只需加载一个文件并将其复制到目录中即可。一切都在我的本地机器上运行,但在我的数字海洋服务器上失败并出现此错误

Class 'SimpleXMLElement' not found

找不到类“SimpleXMLElement”

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

即使在数字海洋服务器上,如果我使用 tinker 运行相同的存储命令,它也能正常工作。可能是什么问题?

My get_loaded_extensions()returns get_loaded_extensions()

我的get_loaded_extensions()回报 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-xmland php-simplexmlshould be installed. Do I need to install something separately?

所以看起来安装了 xml 和 simplexml。正如所建议的链接php-xmlphp-simplexml应安装。我需要单独安装一些东西吗?

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)

回答by nandal

check the version of installed php using:-

使用以下方法检查已安装的 php 版本:-

$ php -v

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

如果是版本 7,则安装php7.0-xml

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

如果是 7.2.* 版,则在您的机器上使用php7.2-xml包和 php。

回答by max johnson

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

只需安装 php-xml 包。作为参考,只需安装所有额外的 php 库:

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

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

回答by AmrataB

Finally, I just had to follow the same answerI 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.

最后,我只需要遵循我在问题中提到的相同答案。必须安装 php7.0-xml 和 simplexml 并重新启动 apache 服务器才能使其工作。因此,即使您加载的扩展显示 xml 和 simplexml 已安装,您可能仍然需要安装 php7.0-xml 和 simplexml。

回答by Piyush Sharma

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

您必须将 \ 斜线放在 SimpleXMLElement (在 Laravel 中)前面才能调用 xml 元素类

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

回答by Pablo Salazar

I solved running this command

我解决了运行这个命令

sudo systemctl restart php-fpm

回答by Sameer Ali

Try

尝试

use SimpleXMLElement;

before calling the class.

在打电话给班级之前。