Created
January 2, 2011 07:03
-
-
Save withgod/762364 to your computer and use it in GitHub Desktop.
php curlを使ったpicasaのアルバム作成の実装
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); | |
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_VERBOSE, 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' => 'password', | |
'source'=>'withgod-sample-2', 'service'=>'lh2'); | |
$auth_ret = doReq("https://www.google.com/accounts/ClientLogin", $param); | |
#var_dump($auth_ret); | |
$tmp1 = preg_split("/[\r\n]+/", $auth_ret); | |
$tmp2 = preg_split("/=/", $tmp1[2]); | |
$auth = $tmp2[1]; | |
return $auth; | |
} | |
$auth = getAuth(); | |
var_dump($auth); | |
$list_ret = doReq('https://picasaweb.google.com/data/feed/api/user/111111111111111111111'); | |
var_dump($list_ret); | |
$atomreq = <<<_EOT_ | |
<entry xmlns='http://www.w3.org/2005/Atom' | |
xmlns:media='http://search.yahoo.com/mrss/' | |
xmlns:gphoto='http://schemas.google.com/photos/2007'> | |
<title type='text'>Trip To Italy</title> | |
<summary type='text'>This was the recent trip I took to Italy.</summary> | |
<gphoto:location>Italy</gphoto:location> | |
<gphoto:access>public</gphoto:access> | |
<gphoto:timestamp>1152255600000</gphoto:timestamp> | |
<media:group> | |
<media:keywords>italy, vacation</media:keywords> | |
</media:group> | |
<category scheme='http://schemas.google.com/g/2005#kind' | |
term='http://schemas.google.com/photos/2007#album'></category> | |
</entry> | |
_EOT_; | |
$create_ret = doReq('https://picasaweb.google.com/data/feed/api/user/111111111111111111111', null, $auth, $atomreq); | |
var_dump($create_ret); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment