找不到类'SimpleXMLElement'

Amr*_*taB 8 php amazon-s3 laravel

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

找不到类'SimpleXMLElement'

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

我的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",
   ]
Run Code Online (Sandbox Code Playgroud)

所以它看起来像是安装了xml和simplexml.如链接所示,应该安装php-xml和php-simplexml.我需要单独安装吗?

另外,我的文件上传代码如下所示

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);
    }
Run Code Online (Sandbox Code Playgroud)

添加异常的堆栈跟踪:

(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)
Run Code Online (Sandbox Code Playgroud)

小智 9

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

$ php -v
Run Code Online (Sandbox Code Playgroud)

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

如果它是版本7.2.*,那么在你的机器上使用php7.2-xml包和php.

  • `sudo apt-get install php7.0-xml` 用于 ubuntu 和 php 7.0 (5认同)

Piy*_*rma 8

你必须在 SimpleXMLElement 前面加上 \ 斜杠(在 Laravel 中)才能调用 xml 元素类

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


小智 7

只需安装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


Amr*_*taB 6

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


Geo*_*oub 6

要解决此问题,请安装最新版本的 php-xml,运行以下命令:

sudo apt-get install php-xml -y
Run Code Online (Sandbox Code Playgroud)

不要忘记重新启动 apache:

sudo systemctl restart apache2
Run Code Online (Sandbox Code Playgroud)


小智 0

尝试

use SimpleXMLElement;
Run Code Online (Sandbox Code Playgroud)

在给班级打电话之前。