90

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.

Promise Preston
  • 8,732
  • 5
  • 43
  • 70
anil
  • 901
  • 1
  • 6
  • 8

7 Answers7

160

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

JohnnyB1988
  • 178
  • 8
Andreas Bergström
  • 11,092
  • 5
  • 49
  • 46
  • 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 Oct 11 '16 at 16:42
  • 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 Oct 26 '16 at 06:16
  • 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 Oct 28 '16 at 17:28
  • yep, this worked, and I was able to skip the first step as @wmfrancia stated. – Linnea Huxford Dec 24 '16 at 20:02
  • 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 Mar 29 '17 at 22:46
  • for php5.6 I uncommented the line as in answer in /etc/php/5.6/cli/php.ini, helped me – Vitali Kuzmin Jun 27 '17 at 19:22
  • saved my day on Raspberry with ubuntu mate – Dmitry Yudin Oct 14 '17 at 15:54
  • I just did "sudo apt install php-xml", no need for other steps – naomi Oct 25 '18 at 11:34
54

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

Fabian Blechschmidt
  • 3,858
  • 19
  • 36
10

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

Promise Preston
  • 8,732
  • 5
  • 43
  • 70
7

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

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

$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:

$result = json_decode($content, true);
$fb_like_count = $result['like_count'];
marian0
  • 3,264
  • 3
  • 27
  • 35
6

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

php-xml or php-simplexml

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

Björn Pfoster
  • 105
  • 1
  • 3
  • 12
  • Also try this [link](http://www.webdeveloper.com/forum/showthread.php?191658-RESOLVED-Fatal-error-Call-to-undefined-function-simplexml_load_file()&s=2a5f7a91ae737417b735ec83fbf17295&p=932942#post932942) – Björn Pfoster Jul 26 '17 at 11:43
2

To fix this error on 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

yesnik
  • 2,425
  • 23
  • 20
2

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

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

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

And restart both Nginx and php

systemctl restart nginx php7.2-fpm
Ajay Singh
  • 634
  • 6
  • 18