带有curl的Google Apps日历忙/闲查询POST

时间:2013-01-10 16:31:01

标签: php post curl google-calendar-api google-apps-for-education

我正在尝试在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日历API的新手。我很乐意为您提供任何帮助/课程/建议!

2 个答案:

答案 0 :(得分:0)

不确定,但你可能首先将php数组转换为json。

$freebusyjson = json_encode($freebusy_post);

答案 1 :(得分:0)

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

请注意我如何在项目变量中格式化日历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);