简体   繁体   English

使用PHP cURL和cron作业获取Facebook评论和共享数

[英]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. 我想知道你们是否可以帮助您查询Facebook 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. 我喜欢每天执行一项Cron作业,根据该帖子具有的Facebook评论数,以“ comment_count”更新我的所有帖子。

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 通常,我收集所有帖子的所有永久链接,并对以下内容发出cURL请求: 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. 但是,有两个问题:1. URL只能这么长。 So, if I try to put all the posts in one URL and send it off, it gets cut off and doesn't work. 因此,如果我尝试将所有帖子放在一个URL中并发送出去,它将被切断并且无法正常工作。 2. The server times out. 2.服务器超时。

Does anyone have a good way to query an API like this and avoid these problems? 有没有人有一种查询这样的API并避免这些问题的好方法?

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: 这可能不在主题之列,而且我不知道您要写入哪个接收器,但是这里有一些未经测试的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 如果仅此,这是对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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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