PHP: Call to undefined function: simplexml_load_string()


86

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

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("abc@gmail.com","hi".$fb_like_count,$message);
}

But I am geting undefined call function error.


6
Do you have php-xml module installed and enabled?
marian0

Respostas:


157

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 Surý's PPA (sudo add-apt-repository ppa:ondrej/php). Go to /etc/php/7.0/fpm and edit php.ini, uncomment to following line:

extension=php_xmlrpc.dll

Then simply install php7.0-xml:

sudo apt-get install php7.0-xml

And restart PHP:

sudo service php7.0-fpm restart

And restart 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).


14
Just fyi you don't need to uncomment extension=php_xmlrpc.dll on Ubuntu as that is a windows extension just do the second step and it will work
wmfrancia

i followed the same : still i get Call to undefined function simplexml_load_string() in /var/www/html/magento1901/lib/Varien/Simplexml/Config.php on line 510
Sushivam

1
I had received a unrecognized service error. I just restarted apache2 and this fixed this issue. Thaks for the help find this line in PHP.ini
Coded Container

yep, this worked, and I was able to skip the first step as @wmfrancia stated.
Linnea Huxford

I tried the same but didn't work for me. From error log Uncaught Error: Call to undefined function simplexml_load_string() in /var/www/html/lib/Varien/Simplexml/Config.php
Killer

52

If the XML module is not installed, install it.

Current version 5.6 on ubuntu 14.04:

sudo apt-get install php5.6-xml

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

Zulhilmi Zainudi


4
You can just do "sudo apt install php-xml" and it will get the correct version for you.
naomi

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

8

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

Here's how I solved it:

Install Simplexml on your Ubuntu Server:

sudo apt-get install php7.2-simplexml

Restart Apache Server

sudo systemctl restart apache2

That's all.

I hope this helps


7

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

Você também pode alterar o formato de resposta para json, que é mais fácil de manusear. Nesse caso, você só precisa adicionar &format=jsona string de consulta do url.

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

E, em seguida, use json_decode()para recuperar dados em seu script:

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

eu modifiquei minha versão do terminal php, então ela está funcionando bem
anil


2

Para corrigir esse erro no Centos 7 :

  1. Instale a extensão PHP:

    sudo yum install php-xml

  2. Reinicie o seu servidor web. No meu caso é php-fpm :

    reiniciar serviços php-fpm


0

Para Nginx (sem apache) e PHP 7.2, instalar php7.2-xml não era suficiente. Tive que instalar o pacote php7.2-simplexml para fazê-lo funcionar

Portanto, os comandos para debian / ubuntu atualizam os pacotes e instalam os dois pacotes

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

E reinicie o Nginx e o php

systemctl restart nginx php7.2-fpm
Ao utilizar nosso site, você reconhece que leu e compreendeu nossa Política de Cookies e nossa Política de Privacidade.
Licensed under cc by-sa 3.0 with attribution required.