How to optimize the facebook likes counter onpage - php

I'm trying to optimize my following code that shows how many likes the page/post on my website have.
This code is looping for 20 posts in my homepage and it's very slow to load.
Is there a way to speed up this code maybe using another call?
Thanks
$url = get_permalink();
$fburl = "http://api.facebook.com/method/fql.query? query=select%20like_count%20from%20link_stat%20where%20url='$url'&format=atom";
$ch = curl_init($fburl);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$atom_data = curl_exec($ch);
preg_match('#like_count>(\d+)<#',$atom_data,$matches);
$like_count = $matches[1];
echo "$like_count";

You can also use an IN list for your URLs like this:
select url, like_count from link_stat where url in ('URL1', 'URL2')
Questions:
Why do you use a deprecated endpoint, and not https://graph.facebook.com/fql?q=...
Why don't you use JSON formatting? This can easily be parsed with PHP and you don't have to use preg_match

You can speed up your code by using batched requests. So you request just one time and not 20 times... https://developers.facebook.com/docs/graph-api/making-multiple-requests/

Related

How to get proper Twitter share count for a URL?

I am implementing a function to get Twitter count for my URL in PHP. I am trying to get count using:
$json_string = file_get_contents("http://urls.api.twitter.com/1/urls/count.json?url=http://themarketmogul.com/will-uber-ipo-in-2015");
$json = json_decode($json_string, true);
echo intval( $json['count'] );
This return 5 count but when I directly hit this URL in the browser then I get absolute count 6.
Please help me to find found proper count in PHP?
The URL you are using is deprecated and won't work anymore. Try http://opensharecount.com to get a URL that will work.

How to make my get Facebook like script loop every 3 seconds?

I am building a Facebook page like counter. I want to be able to get page like updates roughly every 2-3 seconds. The result will be sent to a sql database then sent to an Arduino. First of all though, I need to get the likes from the desired page. I have the PHP script below that works great but you have to load the page every time to get an updated like count.
Would you be able to use a while loop with some sort of 2-3 second delay? or what you recommend in situation? p.s I used Dan Bilzerian's FB page as an example because his likes just keep going up up up, its ridiculous.
Thanks
function facebook_count($url){
// Query in FQL
$fql = "SELECT like_count";
$fql .= " FROM link_stat WHERE url = '$url'";
$fqlURL = "https://api.facebook.com/method/fql.query?format=json&query=" . urlencode($fql);
// Facebook Response is in JSON
$response = file_get_contents($fqlURL);
return json_decode($response);
}
$fb = facebook_count('https://www.facebook.com/danbilzerianofficial');
// facebook like count
echo $fb[0]->like_count;

How can I open a specific frame, using cURL, while also giving a parameter

I'm currently making a site with three frames. Two columns, with the second having two rows.
Frame 1 shows a list of countries from a database, in the form of URLs which open in Frame 3 along with the country's id, so that only regions in that country are selected.
Now what I'm trying to do is have the header also get the country's id, so that I can have it say 'Welcome to $country'. I tried using cURL to open the header, but couldn't figure out how to do this.
My current PHP to retrieve the id(which works) and open the frame(which doesn't work) looks like this:
if(isset($_GET['land']) ){
$country_id = $_GET['land'];
}
else {
$country_id = "1";
}
$ch = curl_init('header.php?land='.$country_id.'target=\'header\'');
curl_exec($ch);
Meanwhile, my header's code looks like following
if(isset($_GET['land']) ){
$country_id = $_GET['land'];
}
else {
$country_id = "1";
}
echo $country_id;
I searched Google, SO and some other sites, but couldn't figure out how to do it. Thanks for either the time to read or respond.
Frames are a web technology from the 80th. You sure you want to use such old stuff? You can, but frames open a whole pothole of problems...
These days one can use client side scripting (ECMA/javascript) for dynamic layout and content manipulation. This gives you much more freedom and you do not get these hard boundaries where your logic is separated.

FB 'pages' have $pageID, what's a website equivalent?

I'm trying to write a simple app for my website and struggling with 1 point.
A Facebook Page has;
$pageID = 'dorimedia';
What does a website have that is equal? I want to get the value to show how many times an external website has been 'liked'.
Nothing.
You could use the domain name, or domain name + path as in full route, but no short ID per se.
http://www.google.com could be the id
http://www.google.com/route could also be an id
If you need something shorter, and not necessarily readable by you, you could hash it or apply some encrypting algorithm to the full url to get a shorter id, try looking into the md5,sha1 or hash methods (assuming your using php)
UPDATE
If what you need is how many facebook like's a website has, there is an API for this directly from google that goes like this:
http://api.facebook.com/restserver.php?method=links.getStats&urls=www.google.com
Which results in an xml response that includes, comments, likes and other interesting information. Google for instance has the following response:
<links_getStats_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://api.facebook.com/1.0/ http://api.facebook.com/1.0/facebook.xsd" list="true">
<link_stat>
<url>www.google.com</url>
<normalized_url>http://www.google.com/</normalized_url>
<share_count>2095550</share_count>
<like_count>795778</like_count>
<comment_count>705595</comment_count>
<total_count>3596923</total_count>
<click_count>265614</click_count>
<comments_fbid>381702034999</comments_fbid>
<commentsbox_count>307</commentsbox_count>
</link_stat>
</links_getStats_response>
Then its a matter of extracting the information from that XML, easily enough like this:
$url = "http://api.facebook.com/restserver.php?method=links.getStats&urls=".urlencode($source_url);
$xml = file_get_contents($url);
$xml = simplexml_load_string($xml);
$shares = $xml->link_stat->share_count;
$likes = $xml->link_stat->like_count;
$comments = $xml->link_stat->comment_count;
$total = $xml->link_stat->total_count;
$max = max($shares,$likes,$comments);
Click here for the Source

How do I make a script to grab certain piece of data from a JSON API?

I'm trying to build a little script that would let me do this:
http://example.com/appicons.php?id=284417350
and then display this in plain text
http://a3.mzstatic.com/us/r1000/005/Purple/2c/a0/b7/mzl.msucaqmg.png
This is the API query to get that information (artworkUrl512):
http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/wa/wsLookup?id=284417350
Any help and example code would be much appreciated!
I am not sure why you have jQuery in your tags, unless you want to make the request dynamically without a page refresh. However you can do this simply in PHP using the following example:
$request = array (
"app_id" => #$_GET["id"]
);
// parse the requests.
if (empty($request["app_id"])) {
// redirects back / displays error
}
else {
$app_uri = "http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/wa/wsLookup?id=" . $request["app_id"];
$data = file_get_contents ($app_uri);
$json = json_decode (trim($data));
print($json->results[0]->artworkUrl100);
}
$request = file_get_contents($itms_url);
$json = json_decode(trim($request));
echo $json[0]->artworkUrl512;
should work in PHP. Unless of course there is more than one hit to the search. A solution using jQuery is probably not very much more difficult.

Categories