如何优化以下标记(php代码-图形api)? 而不是在循环(多个API请求)中这样做,有没有办法在一个API请求中执行它?
,如果同时进行大量操作,请考虑使用。它允许您同时执行多个任务(最多50个) 下面是一个关于理论上如何使用它的示例:如何优化以下标记(php代码-图形api)? 而不是在循环(多个API请求)中这样做,有没有办法在一个API请求中执行它?,php,facebook,facebook-graph-api,Php,Facebook,Facebook Graph Api,,如果同时进行大量操作,请考虑使用。它允许您同时执行多个任务(最多50个) 下面是一个关于理论上如何使用它的示例: foreach($friends_array as $user) { $argstag = array('to' => $user); $argstag['x'] = $locations_x[$i]; $argstag['y'] = $locations_y[$i]; $datatag = $facebook->api('/' . $
foreach($friends_array as $user) {
$argstag = array('to' => $user);
$argstag['x'] = $locations_x[$i];
$argstag['y'] = $locations_y[$i];
$datatag = $facebook->api('/' . $photo_id . '/tags', 'post', $argstag);
$i++;
}
这是没有测试,但希望这是有益的。这可能不是你想要的,但如果你有大量的请求需要发送到Facebook的服务器,那么值得注意。以下代码适用于我,前提是数组$friends少于50个好友:
$batches = array();
$i = $b = 0;
foreach($friends_array as $user) {
$argstag = array('to' => $user);
$argstag['x'] = $locations_x[$i];
$argstag['y'] = $locations_y[$i];
// If we've reached the batch limit, create a new batch request.
if ($i == 50) {
$b++;
$i = 0;
}
// Single batch request.
$batches["$b"][] = array(
'method' => 'POST',
'relative_url' => '/' . $photo_id . '/tags',
'body' => 'to=' . $user . '&x=' . $locations_x[$i] . '&y=' . $locations_y[$i]
);
$i++;
}
if (!empty($batches)) {
foreach ($batches AS $key => $batch) {
$b = json_encode($batch);
$res = $facebook->api('?batch=' . urlencode($b), 'POST');
// Facebook populates $res with the response.
}
}
看一看,花了很长时间。“200 OK 17.28s”。你需要多长时间?为什么标记朋友时速度很慢?我在给五个朋友贴标签。看起来你比平时花的时间要多。我随机标记40个朋友,大约需要20-30秒。我也使用xmlhttprequest来做,所以它在后台保持标记,我不需要等待相同的标记。但是给朋友贴标签也需要很长时间。但是没有标签,它可以很快地将照片上传到facebook。我标记了5个朋友。
$tags = array();
foreach ($friends as $friend)
{
$tag = array();
$tag['tag_uid'] = $friend;
$tag['x'] = rand() % 100;
$tag['y'] = rand() % 100;
$tags[] = $tag;
}
$argstag = array(
'tags' => $tags
);
$facebook->api("$photoId/tags","POST", $argstag);