Created
January 2, 2011 07:02
-
-
Save withgod/762363 to your computer and use it in GitHub Desktop.
php http_request2を使った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 | |
require_once 'HTTP/Request2.php'; | |
function doReq($url, $param = null, $auth = null, $atomreq = null, $debug = false) { | |
//var_dump(array($url, $param, $auth)); | |
$header = array(); | |
$req = mkReq(); | |
$req->setUrl($url); | |
if ($param != null) { | |
$req->setMethod("POST"); | |
$req->addPostParameter($param); | |
} else if ($atomreq != null) { | |
$req->setMethod("POST"); | |
$req->setBody($atomreq); | |
$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) { | |
$req->setHeader($header); | |
} | |
//var_dump($req); | |
try { | |
$ret = $req->send(); | |
if ($debug) { | |
var_dump($ret); | |
} | |
return $ret->getBody(); | |
} catch (HTTP_Request2_Exception $e) { | |
var_dump($e->getMessage()); | |
} catch (Exception $e) { | |
var_dump($e->getMessage()); | |
} | |
} | |
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' => 'password', | |
'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; | |
} | |
function mkReq() { | |
$req = new HTTP_Request2(); | |
$req->setHeader('User-Agent', 'withgod-sample-1'); | |
$req->setConfig(array( | |
'ssl_verify_peer' => false, | |
'follow_redirects' => true | |
)); | |
return $req; | |
} | |
$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, true); | |
//$create_obj = doReq("http://192.168.1.10:8081/fuga/hoge", null, $auth, $createRequest); | |
var_dump($create_obj); | |
//$create = simplexml_load_string($create_obj); | |
//var_dump($create); | |
//exit; | |
//showCalendars($auth); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment