Php Google Apps日历忙/闲查询帖子,带curl
我正在尝试在谷歌资源日历(谷歌应用)上进行忙/闲查询。Freebusy查询的API文档提供了有关请求的以下说明: HTTP请求:Php Google Apps日历忙/闲查询帖子,带curl,php,post,curl,google-calendar-api,google-apps-for-education,Php,Post,Curl,Google Calendar Api,Google Apps For Education,我正在尝试在谷歌资源日历(谷歌应用)上进行忙/闲查询。Freebusy查询的API文档提供了有关请求的以下说明: HTTP请求: POST https://www.googleapis.com/calendar/v3/freeBusy 请求正文结构: { "timeMin": {datetime}, "timeMax": {datetime}, "items": [ {"id": {string} } ] } 下面是我的尝试,使用curl: $u
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和googleapps日历API是全新的。我希望你能提供任何帮助/教训/建议 不确定,但您可能必须首先将php数组转换为json
$freebusyjson = json_encode($freebusy_post);
我从你的代码开始,并对其进行了修改,以下内容对我很有用 注意我是如何在item变量中格式化日历ID的。我还只需要在URL上附加一个API键,因为它是一个公共日历。我认为一开始我并不需要它,但后来我达到了不使用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);
如果var\u dump($data)
显示它是一个布尔值false,那么curl失败了,您需要检查curl\u error()
以了解原因。