Class' SimpleXMLElement'找不到

时间:2017-10-06 16:53:01

标签: php laravel amazon-s3

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

Class' 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",
   ]

所以它看起来像是安装了xml和simplexml。正如link所建议的那样,应该安装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);
    }

添加异常的堆栈跟踪:

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

6 个答案:

答案 0 :(得分:6)

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

$ php -v

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

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

答案 1 :(得分:3)

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

答案 2 :(得分:1)

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

答案 3 :(得分:0)

为了调用xml元素类,您必须在\ simpleXMLElement(在laravel中)前面加\斜杠

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

答案 4 :(得分:0)

我解决了运行此命令的问题

sudo systemctl restart php-fpm

答案 5 :(得分:-2)

尝试

use SimpleXMLElement;

在致电课程之前。