简体   繁体   中英

Using PHP cURL and cron job to get Facebook Comment and Share Count

I was wondering if you guys could help shed some light on best practices when it comes to querying Facebooks API.

I like to run a cron job every day that updates all my posts with a "comment_count" based on the number of Facebook comments the post has.

Generally, I gather all the permalinks of all the posts and make a cURL request to something like this: https://graph.facebook.com/?ids=http://site.com/post/1,http://site.com/post/2,http://site.com/post/3

However, there are two problems 1. A URL can only be so long. So, if I try to put all the posts in one URL and send it off, it gets cut off and doesn't work. 2. The server times out.

Does anyone have a good way to query an API like this and avoid these problems?

Thanks!

This might be off topic, and I don't know which sink you are writing to, but here is some untested code for node.js:

setInterval(updateComments, 3000);

function updateComments()
{
    var ids = ['http://site.com/post1','http://site.com/post2'];

    ids.forEach(function(id){
    var options = { host: 'graph.facebook.com', path: '?ids=' + id, method: 'GET' };
    var req = http.request(options, function(res) {
      var data = "";
      res.on('data',function(chunk){
        data += chunk;
      });
      res.on('end', function(){
        updatePost(id, data);
      });
    });
    req.end();
}

you would still need to batch this, but it would run asynchronously and probably max out your connections...

if nothing more, this is a chorus of praise for node.js

here is how to set it up:

sudo apt-get update
sudo apt-get install git-core curl build-essential openssl libssl-dev
git clone https://github.com/joyent/node.git && cd node
git tag
git checkout [tag of choice/latest:]v0.6.7
./configure
make
sudo make install
node -v
> v0.6.7

echo 'console.log("hello")' >> hello.js && node hello.js
> hello

install NPM (Node Package Manager):
curl http://npmjs.org/install.sh | sudo sh
npm -v
> 1.1.0-beta-10

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM