目前,在我使用的PHP代码库中,有几个时间轴项目和一个捆绑封面插入4个调用中:
insert_timeline_item($mirror_service, $new_timeline_item_1, null, null);
insert_timeline_item($mirror_service, $new_timeline_item_2, null, null);
insert_timeline_item($mirror_service, $new_timeline_item_3, null, null);
insert_timeline_item($mirror_service, $new_timeline_item_bundle_cover, null, null);
我知道Java和Python的方法是在一次批量HTTP调用中将这些全部发送到Mirror API。我如何在PHP中执行此操作?
现在卡片相对缓慢地到达玻璃上,并且用户经常会尝试滚动并看到在其他结果到达之前没有任何东西可以滚动,例如。任何能够帮助结果全部到来的东西都会有很大帮助。我们已经通过仅在最后一张卡上发出通知声音来尽可能地减轻噪音,但它不足以提供良好的用户体验。
答案 0 :(得分:2)
不保证插入时间轴项目的批量请求可以一次性或以任何特定顺序到达。有关批处理操作的文档有一个脚注,指出了这一点(请参阅https://developers.google.com/glass/batch处标题为“对批处理请求的响应”一节末尾的注释)。确保您的卡按顺序到达的唯一方法是在插入时等待每个返回响应,而不是触发最后一张卡以创建带有通知铃声的捆绑包。
编辑:您可以通过将客户端设置为setUseBatch(true)来设置客户端时使用便捷方法(Google_Http_Batch())。请参阅:https://developers.google.com/api-client-library/php/guide/batch
就PHP中的批处理请求而言,据我所知,没有现有的便捷方法。您必须构建自己的请求,通过stream_context_create()设置上下文,然后通过fopen()或file_get_contents()发送。
我没有测试过以下代码,但这是基本概念:
// Our batch url endpoint
$endpoint = "/batch";
// Boundary for our multiple requests
$boundary = "===============SOMETHINGTHATDOESNOTMATCHCONTENT==\n";
//
// Build a series of timeline cards to insert
// probably spin this off into it's own method for simplicty sake
//
$timeline_items .= "--" . $boundary;
$timeline_items .= "Content-Type: application/http\n";
$timeline_items .= "Content-Transfer-Encoding: binary\n";
$timeline_items .= "POST /mirror/v1/timeline HTTP/1.1\n";
$timeline_items .= "Content-Type: application/json\n";
// You'd need the specific ouath2 bearer token for your user here
//
// Note, if you were simply sending a single batch to always one user,
// you could reasonably move this header to the outer /batch params
// as it will flow down to all child requests as per the documentation
//
// see "Format of a batch request" section at https://developers.google.com/glass/batch
//
$timeline_items .= "authorization: Bearer " . $user_bearer_token . "\n";
$timeline_items .= "accept: application/json\n";
$timeline_items .= "content-length: " . strlen($timeline_card_json) . "\n\n";
$timeline_items .= $timeline_card_json . "\n";
$timeline_items .= "--" . $boundary;
//
// Add some other timeline items into your $timeline_items batch
//
// Setup our params for our context
$params = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-Type: multipart/mixed; boundary="' . $boundary . '"',
'accept-encoding' => 'gzip, deflate',
'content' => $timeline_items
)
);
// Create context
$context = stream_context_create($params);
// Fire off request
$batch_result = file_get_contents($endpoint, false, $context);