【问题标题】:Class 'SimpleXMLElement' not found找不到类“SimpleXMLElement”
【发布时间】:2018-03-18 12:59:23
【问题描述】:

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

找不到类“SimpleXMLElement”

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

我的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",
   ]

所以看起来 xml 和 simplexml 已安装。正如linkphp-xmlphp-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);
    }

添加异常的堆栈跟踪:

(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)

【问题讨论】:

  • 检查命名空间,您可能需要使用\SimpleXMLElement调用
  • 它在 AWS 库中的某个地方...在我的代码中没有。
  • 这可能会有所帮助。 stackoverflow.com/a/35834836/3305978
  • 我在我的问题中附加了相同的链接。正如我在扩展列表中显示的那样,xml 和 simplexml 已经存在,但是当我运行 extensions_loaded('php-xml') 时,它返回 false。 php7.0-xml或者simplexml需要单独安装吗?

标签: php laravel amazon-s3


【解决方案1】:

使用以下命令检查已安装 php 的版本:-

$ php -v

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

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

【讨论】:

  • sudo apt-get install php7.0-xml 用于 ubuntu 和 php 7.0
  • 我在 Ubuntu 20.04 上使用 php 8,并且我的服务器上安装了 php-xml,但这并不能解决问题
【解决方案2】:

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

【讨论】:

  • 我在 Ubuntu 20.04 上使用 php 8 并安装了所有提到的软件包,但这并没有解决问题
【解决方案3】:

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

【讨论】:

    【解决方案4】:

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

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

    【讨论】:

      【解决方案5】:

      我解决了运行这个命令

      sudo systemctl restart php-fpm
      

      【讨论】:

        【解决方案6】:

        试试

        use SimpleXMLElement;
        

        在调用类之前。

        【讨论】:

        • 在我的文件中?我没有直接使用这个类,而是在他们使用它的库中,他们实际上是在正确使用它。但不管怎样,我试过了,还是不行。
        • 如果您在另一个类中使用该类(OOP 方法),这将很有帮助
        【解决方案7】:

        我只需要在安装后重新启动 apache2

        systemctl apache2 重启

        【讨论】:

          【解决方案8】:

          要解决这个问题,请安装最新版本的 php-xml,运行:

          sudo apt-get install php-xml -y
          

          别忘了重启apache:

          sudo systemctl restart apache2
          

          【讨论】:

            猜你喜欢
            • 2016-05-15
            • 2020-11-10
            • 2016-11-14
            • 2014-10-19
            • 2014-03-01
            • 1970-01-01
            • 2021-05-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多