php 调用未定义的函数:simplexml_load_string()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31206186/
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-08-25 22:18:18  来源:igfitidea点击:

Call to undefined function: simplexml_load_string()

phpxmlfunctioncurl

提问by anil

I am implementing facebook count function using cron file. In which cron runs every 10 minutes and counts the total likes of a page.

我正在使用 cron 文件实现 Facebook 计数功能。其中 cron 每 10 分钟运行一次,并计算页面的总点赞数。

for($i=0;$i<3;$i++){
    $source_url =$cars[$i];
    $rest_url = "http://api.facebook.com/restserver.php?method=links.getStats&urls=".urlencode($source_url);
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL,$rest_url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $content = curl_exec($curl);
    curl_close($curl);
    $message=stripslashes($content);
    $xml_record = simplexml_load_string($message);
    $fb_like_count = $xml_record->link_stat->like_count;
    echo "".$fb_like_count;
    mail("[email protected]","hi".$fb_like_count,$message);
}

But I am geting undefined call function error.

但我收到未定义的调用函数错误。

回答by Andreas Bergstr?m

For PHP 7 and Ubuntu 14.04 the procedure is follows. Since PHP 7 is not in the official Ubuntu PPAs you likely installed it through Ond?ej Sury's PPA (sudo add-apt-repository ppa:ondrej/php). Go to /etc/php/7.0/fpm and edit php.ini, uncomment to following line:

对于 PHP 7 和 Ubuntu 14.04,过程如下。由于 PHP 7 不在官方 Ubuntu PPA 中,您可能是通过 Ond?ej Sury 的 PPA(sudo add-apt-repository ppa:ondrej/php)安装的。转到 /etc/php/7.0/fpm 并编辑 php.ini,取消注释以下行:

extension=php_xmlrpc.dll

Then simply install php7.0-xml:

然后只需安装php7.0-xml:

sudo apt-get install php7.0-xml

And restart PHP:

并重新启动 PHP:

sudo service php7.0-fpm restart

And restart Apache:

并重新启动Apache:

sudo service apache2 restart

If you are on a later Ubuntu version where PHP 7 is included, the procedure is most likely the same as well (except adding any 3rd party repository).

如果您使用的是包含 PHP 7 的更高版本的 Ubuntu,则该过程很可能也是相同的(除了添加任何 3rd 方存储库)。

回答by Fabian Blechschmidt

If the XML module is not installed, install it.

如果未安装 XML 模块,请安装它。

Current version 5.6 on ubuntu 14.04:

ubuntu 14.04 上的当前版本 5.6:

sudo apt-get install php5.6-xml

And don't forget to run sudo service apache2 restart command after it

并且不要忘记在它之后运行 sudo service apache2 restart 命令

Zulhilmi Zainudi

祖利米再努迪

回答by Promise Preston

I also faced this issue. My Operating system is Ubuntu 18.04and my PHP version is PHP 7.2.

我也遇到过这个问题。我的操作系统是Ubuntu 18.04而我的 PHP 版本是PHP 7.2

Here's how I solved it:

这是我解决它的方法:

Install Simplexml on your Ubuntu Server:

在你的 Ubuntu 服务器上安装 Simplexml:

sudo apt-get install php7.2-simplexml

Restart Apache Server

重启 Apache 服务器

sudo systemctl restart apache2

That's all.

就这样。

I hope this helps

我希望这有帮助

回答by Bj?rn Pfoster

I think it can be something like in this Post: Class 'SimpleXMLElement' not found on puphpet PHP 5.6So maybe you could install/activate

我认为它可能类似于这篇文章: 在 puphpet PHP 5.6 上找不到 Class 'SimpleXMLElement'所以也许你可以安装/激活

php-xml or php-simplexml

Do not forget to activate the libraries in the php.inifile. (like the top comment)

不要忘记激活php.ini文件中的库。(如置顶评论

回答by marian0

Make sure that you have php-xml module installed and enabled in php.ini.

确保在 .php 中安装并启用了 php-xml 模块php.ini

You can also change response format to json which is easier to handle. In that case you have to only add &format=jsonto url query string.

您还可以将响应格式更改为更易于处理的 json。在这种情况下,您只需添加&format=json到 url 查询字符串。

$rest_url = "http://api.facebook.com/restserver.php?method=links.getStats&format=json&urls=".urlencode($source_url);

And then use json_decode()to retrieve data in your script:

然后用于json_decode()检索脚本中的数据:

$result = json_decode($content, true);
$fb_like_count = $result['like_count'];

回答by yesnik

To fix this error on Centos 7:

要在Centos 7上修复此错误:

  1. Install PHP extension:

    sudo yum install php-xml

  2. Restart your web server. In my case it's php-fpm:

    services php-fpm restart

  1. 安装 PHP 扩展:

    须藤 yum 安装 php-xml

  2. 重新启动您的网络服务器。就我而言,它是php-fpm

    服务 php-fpm 重启

回答by Ajay Singh

For Nginx (without apache) and PHP 7.2, installing php7.2-xml wasn't enough. Had to install php7.2-simplexml package to get it to work

对于 Nginx(没有 apache)和 PHP 7.2,安装 php7.2-xml 是不够的。必须安装 php7.2-simplexml 包才能使其工作

So the commands for debian/ubuntu, update packages and install both packages

所以 debian/ubuntu 的命令,更新包并安装这两个包

apt update
apt install php7.2-xml php7.2-simplexml

And restart both Nginx and php

并重新启动 Nginx 和 php

systemctl restart nginx php7.2-fpm