如何使用PHP和Google Glass Mirror API批量插入时间线项目?

如何使用PHP和Google Glass Mirror API批量插入时间线项目?,php,google-glass,google-mirror-api,Php,Google Glass,Google Mirror Api,目前,在我处理的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

目前,在我处理的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调用将这些内容发送到镜像API的方法。如何在PHP中实现这一点


现在,卡片到达玻璃上的速度相对较慢,例如,用户通常会尝试滚动,并在其他结果到达之前看到没有任何东西可以滚动。任何有助于所有结果同时到达的东西都会有很大帮助。我们已经通过只在最后一张卡上发出通知声音来尽可能地减轻压力,但这还不足以获得良好的用户体验。

插入时间线项目的批处理请求不能保证一次或以任何特定顺序到达。有关批处理操作的文档有一个脚注指出了这一点(请参阅“对批处理请求的响应”一节末尾的注释)。确保您的卡按顺序到达的唯一方法是等待插入时的每个返回响应,而不是启动最后一张卡以创建带有通知提示的捆绑包

编辑:当您通过将客户端设置为setUseBatch(true)来设置客户端时,可以使用一种方便的方法(Google_Http_Batch())。见:

就PHP中的批处理请求而言,据我所知,目前还没有一种方便的方法。您必须构建自己的请求,通过stream\u context\u create()设置上下文,然后通过fopen()或file\u get\u contents()发送它

我没有测试以下代码,但这是基本概念:


最终使用以下语法()使setUseBatch正常工作。此修复程序返回请求(),此修复程序使私有方法公开()。谢谢
// 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);