Created
January 2, 2011 06:59
-
-
Save withgod/762362 to your computer and use it in GitHub Desktop.
php curlを使ったgoogle calendar 新規作成の実装
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function doReq($url, $param = null, $auth = null, $atomreq = null) { | |
//var_dump(array($url, $param, $auth)); | |
$header = array(); | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); | |
curl_setopt($ch, CURLOPT_VERBOSE, true); | |
if ($param != null) { | |
curl_setopt($ch, CURLOPT_POST, true); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $param); | |
} else if ($atomreq != null) { | |
curl_setopt($ch, CURLOPT_POST, true); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $atomreq); | |
//一般的では無いオプション | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); | |
curl_setopt($ch, CURLOPT_FAILONERROR, true); | |
//curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie"); | |
//curl_setopt($ch, CURLOPT_COOKIEFILE, "tmp"); | |
// | |
$header[] = 'Content-Type: application/atom+xml;charset=UTF-8'; | |
$header[] = 'Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2'; | |
$header[] = 'Pragma: no-cache'; | |
$header[] = 'Cache-Control: no-cache'; | |
$header[] = 'GData-Version: 2.0'; | |
$header[] = 'Content-Length: ' . strlen($atomreq); | |
$header[] = 'User-Agent: withgod-sample-1'; | |
//とりあえず付けてみた | |
$header[] = 'Accept-Encoding: gzip'; | |
$header[] = 'Connection: keep-alive'; | |
} | |
if ($auth != null) { | |
$header[] = "Authorization: GoogleLogin auth=$auth"; | |
} | |
if (sizeof($header) > 0) { | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); | |
} | |
$ret = curl_exec($ch); | |
curl_close($ch); | |
return $ret; | |
} | |
function showCalendars($auth) { | |
$list_obj = doReq("https://www.google.com/calendar/feeds/default/owncalendars/full", null, $auth); | |
$list = simplexml_load_string($list_obj); | |
print "calendar list\n"; | |
foreach ($list->entry as $entry) { | |
var_dump(array((string)$entry->id, (string)$entry->title, (string)$entry->author->name)); | |
} | |
} | |
function getAuth() { | |
$param = array( | |
'accountType' => 'GOOGLE', | |
'Email' => '[email protected]', | |
'Passwd' => 'passowrd', | |
'source'=>'withgod-sample-1', 'service'=>'cl'); | |
$auth_ret = doReq("https://www.google.com/accounts/ClientLogin", $param); | |
$tmp1 = preg_split("/[\r\n]+/", $auth_ret); | |
$tmp2 = preg_split("/=/", $tmp1[2]); | |
$auth = $tmp2[1]; | |
return $auth; | |
} | |
$auth = getAuth(); | |
var_dump($auth); | |
showCalendars($auth); | |
$createRequest = "<?xml version='1.0' encoding='UTF-8'?><entry xmlns='http://www.w3.org/2005/Atom' xmlns:gAcl='http://schemas.google.com/acl/2007' xmlns:batch='http://schemas.google.com/gdata/batch' xmlns:gCal='http://schemas.google.com/gCal/2005' xmlns:gd='http://schemas.google.com/g/2005'><title>Little League Schedule2</title><atom:summary xmlns:atom='http://www.w3.org/2005/Atom'>xx This calendar contains the practice schedule and game times.</atom:summary><gCal:timezone value='America/Los_Angeles'/><gCal:hidden value='false'/><gCal:color value='#2952A3'/><gd:where rel='' label='' valueString='Oakland'/></entry>"; | |
$create_obj = doReq("https://www.google.com/calendar/feeds/default/owncalendars/full", null, $auth, $createRequest); | |
//$create_obj = doReq("http://192.168.1.10:8081/", null, $auth, $createRequest); | |
var_dump($create_obj); | |
$create = simplexml_load_string($create_obj); | |
var_dump($create); | |
showCalendars($auth); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment