0

我正在尝试在 Google 资源日历(Google Apps)上进行空闲/忙碌查询。Freebusy 查询的 API 文档提供了有关请求的以下说明:

HTTP 请求:

POST https://www.googleapis.com/calendar/v3/freeBusy

请求正文结构:

{  "timeMin": {datetime},
   "timeMax": {datetime},
   "items": [
      {"id": {string}
      }
    ]
}

这是我的尝试,使用 curl:

$url = "https://www.googleapis.com/calendar/v3/freeBusy";
global $headers;  // holds ClientLogin authentification

$freebusy_post = array(

        "timeMin" => '2012-12-31T00:00:00',
        "timeMax" => '2013-01-08T23:00:00',
        "timeZone" => 'America/New York',

/*** "items" below commented out because posting a multidimensional array returns an 
"Array to string conversion" error. However, I think the API requires 
"items" to be an array. ***/

        //"items" => array(
        //        "id" => "https://www.googleapis.com/calendar/v3/calendars/gdev.wellesley.edu_313736333535343332@resource.calendar.google.com" 
        //        )

/*** My attempt at using a plain string instead of an array ***/

        "items" => "gdev.wellesley.edu_313736333535343332@resource.calendar.google.com",        

); 

$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $freebusy_post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url);

$data = curl_exec($ch);
print_r($data);

我的问题: $data 不打印任何内容。

我是 PHP、curl 和 Google Apps Calendar API 的新手。我很想你能提供任何帮助/课程/建议!

4

2 回答 2

0

Not sure, but you have probably to convert the php array to a json first.

$freebusyjson = json_encode($freebusy_post);
于 2013-04-08T15:09:14.037 回答
0

我从您的代码开始并对其进行了修改,以下内容对我有用...

请注意我如何在 item 变量中格式化日历 ID。我还只需要将 API 密钥附加到 URL,因为它是公共日历。我认为我最初不需要它,但后来我达到了不使用 API 密钥可以发出的请求数量。

最后,注意时间/日期的格式。我说这是唯一的方法,但它对我有用。

$url = "https://www.googleapis.com/calendar/v3/freeBusy/?key=MY_API_KEY";

$freebusy_post = array(
        "timeMin" => '2016-11-01T00:00:00.000Z',
        "timeMax" => '2016-12-21T00:00:00.000Z',
        "timeZone" => ' America/New York',
        "items" => array(array('id'=>"mywebsite.com_randomlettershere@group.calendar.google.com")),        

);

$freebusy_post = json_encode($freebusy_post);
$ch = curl_init();curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $freebusy_post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_URL, $url);

$data = curl_exec($ch);
print_r($data);
于 2016-12-09T15:12:32.980 回答