Jump to content

[solved] add facebook fans count in a smarty template


Recommended Posts

Hi,

 

I'm looking to add the number of my facebook's fans into my prestashop template. As a smarty variable, so it updates everytime.

 

I have found some code to do it in php :

 

<?php
require_once('facebook.php');
$facebook = new Facebook(array(
'appId'  => 'your_app_id',
'secret' => 'your_app_secret',
'cookie' => true,
));
$result = $facebook->api(array(
'method' => 'fql.query',
'query' => 'select fan_count from page where page_id = your_page_id;'
));
$fb_fans = $result[0]['fan_count'];
?>

 

or simpler :

<?php
$page_id = "XXXXXXXXXXXX";
$xml = @simplexml_load_file("http://api.facebook.com/restserver.php?method=facebook.fql.query&query=SELECT%20fan_count%20FROM%20page%20WHERE%20page_id=".$page_id."") or die ("a lot");
$fans = $xml->page->fan_count;
echo $fans;
?>

 

What's the best way to add the PHP to the smarty template .tpl ? for then use the variable to show the data ?

 

Thanks for any help,

 

Rodrigo

Link to comment
Share on other sites

it depends on where you want to display it. Generally speaking - in my opinion - it's better to NOT use php code in the smarty files (for now it isn't possible - {php} {/php} doesnt work now)

 

so, you have to add php code to the controller / class (it depends on place, where you want to display likes count)

then you can add variable with counter to the smarty array - this will allow you to use it in .tpl file

 

if it is possible - please let me know where you want to display it - and i will show you the guide to achieve what you want :-)

 

regards

Link to comment
Share on other sites

okay

 

i've got simplest method than yours:

 

go to the file:

 

classes/controller/FrontController.php

 

add somewhere in the class this function:

   public static function fbcount($idpage){
    $ch = curl_init("http://graph.facebook.com/$idpage");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $raw = curl_exec($ch);
    curl_close($ch);

    $data = json_decode($raw);
    return $data->likes;
   }

 

then in the footer.tpl file you can use this:

{FrontController::fbcount('399888213399907')}

 

where the 399888213399907 is id of your page

 

enjoy! :)

Link to comment
Share on other sites

You're welcome :-)

This method is easiest that method with facebook api & soap calls

No app id, no worries

it works like a charm as you said :-)

 

so, now i can mark this thread as solved

 

regards

  • Like 1
Link to comment
Share on other sites

  • 3 weeks later...

okay

 

i've got simplest method than yours:

 

go to the file:

 

classes/controller/FrontController.php

 

add somewhere in the class this function:

public static function fbcount($idpage){
	$ch = curl_init("http://graph.facebook.com/$idpage");
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
	$raw = curl_exec($ch);
	curl_close($ch);

	$data = json_decode($raw);
	return $data->likes;
}

 

then in the footer.tpl file you can use this:

{FrontController::fbcount('399888213399907')}

 

where the 399888213399907 is id of your page

 

enjoy! :)

 

 

Hello Vekia,

 

Thanks a lot for this code method....

 

i have a little question ... for the same likes count for each page product or any page, what is the solution ?

I need to create a little button with the like for each product with url.

 

Thank you again

Matias

Link to comment
Share on other sites

you can use the same method but with modifications, sorry to bother you, but your question isn't the same as main case in this thread - so if it is possible, please create new thread with your question and i will write solution especiall for you ;)

Link to comment
Share on other sites

  • 4 months later...

Ok Vekia 

 

Thanks 

 

 

ok, i've got it!

    public static function tcount($url){
		$ch = curl_init("http://urls.api.twitter.com/1/urls/count.json?url=$url");
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		$raw = curl_exec($ch);
		curl_close($ch);
		$data = json_decode($raw);
		return $data->count;
	}

+ in template file to disply "tweets" count for each page in the shop, use this:

{FrontController::tcount("{$smarty.server.HTTP_HOST}{$smarty.server.REQUEST_URI}")}
Link to comment
Share on other sites

  • 2 weeks later...
  • 2 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...