我从graph.facebook.com获得了Facebook数据而没有使用CURL做一些PHP随机性......:)
它有点横向但我不知道如何使用CURL而且我也不需要传递任何应用程序密钥或任何其他等等!
// Create a function to calculate the number of fans.
function calculatefans($facebookid) {
$file = "https://graph.facebook.com/".$facebookid;
// Get data from that specific Facebook page
$facebookdata = file_get_contents($file);
// Reverse data from Facebook Graph so that the "likes" figure is at the front
$facebookbackwards = strrev($facebookdata);
// Select only the "likes" figure is in the variable
$offset = strpos($facebookbackwards, ":");
// Minus 1 from the offset
$newoffset = $offset-1;
$fansbackwards = substr($facebookbackwards, 1, $newoffset);
// Turn the "likes" figure the right way around
$fans = strrev($fansbackwards);
// Change the result from a string to an integer so you can do some maths on the result.
$fanresult = (int)$fans;
// Use return not echo because you want to do something with the data later.
return $fanresult;
}
然而,这显然是非常缓慢地运行...我想知道是否有人对如何加快这一点有任何想法?
我正在运行的网站是http://www.ibizavote.com。如果您看到,代码正在运行,但是当我将其添加到站点时,站点从4.4secs加载到14.4秒。
我确信我可以加快速度...... 有什么想法吗?
谢谢, 亚历
答案 0 :(得分:0)
str反转,substr,strpos非常快。
缓慢的部分将是Web服务请求。如果您使用curl执行此操作,则可以按照此处的示例同时发送多个请求并将其调整为Facebook apis:http://php.net/manual/en/function.curl-multi-init.php。
只是提醒一下:为什么你不将Facebook响应解析为JSON数据? json_decode在c中实现,也很快。如果Facebook的输出格式发生变化,那么你在这里进行的字符串抓取很容易失败。
此外,您可能希望查看FQL,您可以在其中生成更复杂的报告,并仅返回您正在查找的数据子集。请参阅:http://developers.facebook.com/docs/reference/fql/
在这种情况下你要做的另一件事就是缓存来自Facebook的回复,这样你就不必在每次请求时都询问他们所有的粉丝数。
并行请求(curl_multi_init)+缓存是可行的方法。