{"id":1505,"date":"2015-04-05T09:46:18","date_gmt":"2015-04-05T00:46:18","guid":{"rendered":"http:\/\/kazu.tv\/blog\/?p=1505"},"modified":"2015-04-05T09:52:49","modified_gmt":"2015-04-05T00:52:49","slug":"use-google-spreadsheets-api-in-php","status":"publish","type":"post","link":"https:\/\/kazu.tv\/blog\/2015\/04\/05\/use-google-spreadsheets-api-in-php\/","title":{"rendered":"PHP\u304b\u3089Google Spreadsheets API\u3092\u4f7f\u3046"},"content":{"rendered":"<p>\u540c\u3058\u3053\u3068\u3084\u308b\u306e2\u5ea6\u76ee\u306a\u306e\u306b\u3001\u3059\u3063\u304b\u308a\u5fd8\u308c\u3066\u3057\u307e\u3063\u3066\u3044\u305f\u306e\u3067\u30013\u5ea6\u3081\u306b\u5099\u3048\u3066\u30e1\u30e2\u3002<\/p>\n<h2>\u3084\u308a\u305f\u3044\u3053\u3068<\/h2>\n<p>\uff08MySQL\u306b\u5165\u3063\u3066\u3044\u308b\uff09\u3042\u308b\u30c7\u30fc\u30bf\u3092\u3001Google Spreadsheets API\u3092\u4f7f\u3063\u3066\u30b9\u30d7\u30ec\u30c3\u30c9\u30b7\u30fc\u30c8\u306b\u66f8\u304d\u8fbc\u3080\u3002<\/p>\n<h2>\u4f7f\u3046\u3082\u306e<\/h2>\n<p>\u4ee5\u4e0b\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u4f7f\u3046\u3002<\/p>\n<ul>\n<li><a title=\"Google APIs Client Library for PHP\" href=\"https:\/\/github.com\/google\/google-api-php-client\" target=\"_blank\">Google APIs Client Library for PHP (google-api-php-client)<\/a><\/li>\n<li><a title=\"php-google-spreadsheet-client\" href=\"https:\/\/github.com\/asimlqt\/php-google-spreadsheet-client\" target=\"_blank\">php-google-spreadsheet-client<\/a><\/li>\n<\/ul>\n<p><!--more--><\/p>\n<h2>\u4e8b\u524d\u6e96\u5099<\/h2>\n<h3>API\u306e\u6709\u52b9\u5316<\/h3>\n<p>\u307e\u3060\u306e\u4eba\u306f\u3001<a title=\"API Console - Google Code\" href=\"https:\/\/code.google.com\/apis\/console\" target=\"_blank\">Google API Console<\/a>\u304b\u3089\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3092\u4f5c\u6210\u3002<\/p>\n<p>\u6b21\u306b\u3001\u5de6\u306e\u30e1\u30cb\u30e5\u30fc\u304b\u3089\u300cAPI\u300d\u3092\u9078\u629e\u3057\u3066\u3001\u5fc5\u8981\u306aAPI\u3092\u6709\u52b9\u306b\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3093\u3060\u3051\u3069\u3001\u30b9\u30d7\u30ec\u30c3\u30c9\u30b7\u30fc\u30c8\u3092\u4f7f\u3046\u306b\u306f&#8221;Drive API&#8221;\u3092\u6709\u52b9\u306b\u3057\u3066\u304a\u3051\u3070\u826f\u3055\u305d\u3046\u3002<\/p>\n<h3>\u30b5\u30fc\u30d3\u30b9\u30a2\u30ab\u30a6\u30f3\u30c8\u306e\u4f5c\u6210<\/h3>\n<p>\u6700\u5f8c\u306b\u3001API\u7528\u306e\u300c\u30a2\u30ab\u30a6\u30f3\u30c8\u300d\u3092\u4f5c\u6210\u3002API\u3092\u4f7f\u3046\u65b9\u6cd5\u3068\u3057\u3066\u3001\u30e6\u30fc\u30b6\u30fc\uff08Google\u30a2\u30ab\u30a6\u30f3\u30c8\u3092\u6301\u3063\u3066\u3044\u308b\u4eba\u9593\uff09\u306e\u6a29\u9650\u3067\u30a2\u30af\u30bb\u30b9\u3059\u308b\u65b9\u6cd5\u3068\u3001\u30b7\u30b9\u30c6\u30e0\u30a2\u30ab\u30a6\u30f3\u30c8\u306e\u3088\u3046\u306a\u3082\u306e\u3092\u4f5c\u3063\u3066\u305d\u306e\u6a29\u9650\u3067\u30a2\u30af\u30bb\u30b9\u3059\u308b\u65b9\u6cd5\u306e2\u901a\u308a\u304c\u3042\u308b\u3002\u3061\u306a\u307f\u306b\u3001\u524d\u8005\u306fWeb\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3067\u3088\u304f\u4f7f\u308f\u308c\u308bOAuth\u3092\u4f7f\u3046\u3002<\/p>\n<p>\u4eca\u56de\u306f\u3001\u30d0\u30c3\u30c1\u51e6\u7406\u3067\u5b9a\u671f\u7684\u306bMySQL\u30c7\u30fc\u30bf\u3092Google\u30b9\u30d7\u30ec\u30c3\u30c9\u30b7\u30fc\u30c8\u306b\u66f8\u304d\u8fbc\u3080\u306e\u3067\u3001\u5f8c\u8005\u306e\u300c\u30b5\u30fc\u30d3\u30b9\u30a2\u30ab\u30a6\u30f3\u30c8\u300d\u3092\u4f7f\u3046\u65b9\u6cd5\u3092\u4f7f\u7528\u3002<\/p>\n<p>\u4ee5\u4e0b\u3001\u624b\u9806\u3002<\/p>\n<ul>\n<li>API\u30b3\u30f3\u30bd\u30fc\u30eb\u306e\u5de6\u306e\u30e1\u30cb\u30e5\u30fc\u304b\u3089\u300c\u8a8d\u8a3c\u60c5\u5831\u300d\u3092\u9078\u629e<\/li>\n<li>\u53f3\u306e\u30e1\u30a4\u30f3\u753b\u9762\u304b\u3089\u300c\u65b0\u3057\u3044\u30af\u30e9\u30a4\u30a2\u30f3\u30c8ID\u3092\u4f5c\u6210\u300d\u3092\u30af\u30ea\u30c3\u30af<\/li>\n<li>\u300c\u30b5\u30fc\u30d3\u30b9\u30a2\u30ab\u30a6\u30f3\u30c8\u300d\u3092\u9078\u629e<\/li>\n<li>JSON\u30ad\u30fc\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u51fa\u6765\u308b\u3088\u3046\u306b\u306a\u308b\u304c\u3001\u3053\u308c\u306f\u591a\u5206\u4f7f\u308f\u306a\u3044\u306e\u3067\u3001\u300c\u65b0\u3057\u3044P12\u30ad\u30fc\u3092\u4f5c\u6210\u300d\u3092\u30af\u30ea\u30c3\u30af\u3057\u3001\u9375\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3002\u5931\u304f\u3055\u306a\u3044\u3088\u3046\u306b\u8981\u6ce8\u610f\u3002<\/li>\n<\/ul>\n<h3>\u30b9\u30d7\u30ec\u30c3\u30c9\u30b7\u30fc\u30c8\u306e\u6a29\u9650\u3092\u8a2d\u5b9a<\/h3>\n<p>\u8aad\u307f\u66f8\u304d\u3057\u305f\u3044\u30b9\u30d7\u30ec\u30c3\u30c9\u30b7\u30fc\u30c8\u306b\u5bfe\u3057\u3066\u3001\u30b5\u30fc\u30d3\u30b9\u30a2\u30ab\u30a6\u30f3\u30c8\u304b\u3089\u30a2\u30af\u30bb\u30b9\u3067\u304d\u308b\u5fc5\u8981\u304c\u3042\u308b\u305f\u3081\u3001\u300c\u5171\u6709\u300d\u8a2d\u5b9a\u3067\u3001\u5148\u307b\u3069\u4f5c\u6210\u3057\u305f\u30b5\u30fc\u30d3\u30b9\u30a2\u30ab\u30a6\u30f3\u30c8\u306e\u30e1\u30fc\u30eb\u30a2\u30c9\u30ec\u30b9\uff08\u4f8b\uff1a 123456789012-asdfghjkl1234567890qwertyuiop@developer.gserviceaccount.com \uff09\u306b\u5bfe\u3057\u3066\u30a2\u30af\u30bb\u30b9\u8a31\u53ef\u3092\u4e0e\u3048\u308b\u5fc5\u8981\u304c\u3042\u308b\u3002<\/p>\n<p>\u3053\u308c\u3067\u6e96\u5099\u5b8c\u4e86\u3002<\/p>\n<h2>\u30d7\u30ed\u30b0\u30e9\u30e0<\/h2>\n<p>\u5927\u307e\u304b\u306a\u6d41\u308c\u3068\u3057\u3066\u3001access token\u306e\u4f5c\u6210\u307e\u3067\u306fGoogle APIs Client Library for PHP\u3092\u4f7f\u7528\u3057\u3001\u5b9f\u969b\u306e\u30b9\u30d7\u30ec\u30c3\u30c9\u30b7\u30fc\u30c8\u3078\u306e\u30a2\u30af\u30bb\u30b9\u306b\u306f<a title=\"php-google-spreadsheet-client\" href=\"https:\/\/github.com\/asimlqt\/php-google-spreadsheet-client\" target=\"_blank\">php-google-spreadsheet-client<\/a>\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u4f7f\u7528\u3059\u308b\u3002<\/p>\n<h3>\u521d\u671f\u5316\u301c\u30a2\u30af\u30bb\u30b9\u30c8\u30fc\u30af\u30f3\u53d6\u5f97<\/h3>\n<pre class=\"brush: php;\">$client = new Google_Client();\r\n$client-&gt;setApplicationName(\"Great Application\");\r\n\r\n\/\/ \u4ee5\u4e0b\u306e\u5024\u306f\u3001API Console\u3088\u308a\u78ba\u8a8d\u3059\u308b\r\n$clientId = '123456789012-asdfghjkl1234567890qwertyuiop.apps.googleusercontent.com';\r\n$serviceAccountName = '123456789012-asdfghjkl1234567890qwertyuiop@developer.gserviceaccount.com';\r\n$keyFile = '\/path\/to\/greate-app-00ab12345678.p12';\r\n\r\n\/\/ Google_Client \u3092\u521d\u671f\u5316\r\n$client-&gt;setClientId($clientId);\r\n$credentials = new Google_Auth_AssertionCredentials(\r\n\u00a0\u00a0\u00a0 $serviceAccountName,\r\n\u00a0\u00a0\u00a0 $scopes,\r\n\u00a0\u00a0\u00a0 file_get_contents($keyFile)\r\n);\r\n$client-&gt;setAssertionCredentials($credentials);\r\n\r\n\/\/ access token \u53d6\u5f97\r\nif ($client-&gt;getAuth()-&gt;isAccessTokenExpired()) {\r\n\u00a0\u00a0\u00a0 $client-&gt;getAuth()-&gt;refreshTokenWithAssertion($credentials);\r\n}\r\n$objToken = json_decode($client-&gt;getAccessToken());\r\n$accessToken = $objToken-&gt;access_token;\r\n<\/pre>\n<h3>\u30b9\u30d7\u30ec\u30c3\u30c9\u30b7\u30fc\u30c8\u306b\u30a2\u30af\u30bb\u30b9<\/h3>\n<p>\u521d\u671f\u5316\u304c\u5fc5\u8981\u3089\u3057\u3044\u3002<\/p>\n<pre class=\"brush: php;\">$serviceRequest = new DefaultServiceRequest($accessToken);\r\nServiceRequestFactory::setInstance($serviceRequest);<\/pre>\n<p>\u3053\u3053\u307e\u3067\u3084\u308c\u3070\u3001\u3042\u3068\u306f\u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\uff08README\uff09\u3092\u898b\u308c\u3070\u554f\u984c\u306a\u3044\u304b\u3068\u601d\u3046\u304c\u3001\u4e00\u5fdc\u4f8b\u3092\u6319\u3052\u3066\u304a\u304f\u3002<\/p>\n<pre class=\"brush: php;\">$spreadsheetService = new Google\\Spreadsheet\\SpreadsheetService();\r\n\/\/ \"ID\"\u306f\u3001\u30b9\u30d7\u30ec\u30c3\u30c9\u30b7\u30fc\u30c8\u306eURL\u3092\u898b\u308c\u3070\u5206\u304b\u308b\r\n$spreadsheet = $spreadsheetService-&gt;getSpreadsheetById('1pyNsD8mTr7ufThnGY9dvfB3HF9mJ45TLgqvAbdG-Q4G');\r\n$worksheetFeed = $spreadsheet-&gt;getWorksheets();\r\n$worksheet = $worksheetFeed-&gt;getByTitle(\"\u51fa\u529b\u5148\u30b7\u30fc\u30c8\");<\/pre>\n<h2>\u307e\u3068\u3081<\/h2>\n<p>\u975e\u6280\u8853\u8005\u306bMySQL\u306e\u30c6\u30fc\u30d6\u30eb\u3092\u76f4\u63a5\u898b\u305b\u308b\u306e\u306b\u306f\u62b5\u6297\u304c\u3042\u308b\u5834\u5408\u306a\u3069\u3001\u30b9\u30d7\u30ec\u30c3\u30c9\u30b7\u30fc\u30c8\u306b\u51fa\u529b\u3057\u305f\u3044\u5834\u9762\u306f\u7d50\u69cb\u3042\u308b\u3068\u601d\u3046\u3002\u305d\u3046\u3044\u3046\u6642\u306b\u3001PHP\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u4f7f\u3063\u3066\u6bd4\u8f03\u7684\u7c21\u5358\u306bGoogle\u30b9\u30d7\u30ec\u30c3\u30c9\u30b7\u30fc\u30c8\u306b\u30c7\u30fc\u30bf\u306e\u51fa\u529b\u304c\u51fa\u6765\u308b\u3002<\/p>\n<p>\u7528\u8a9e\u304c\u5206\u304b\u308a\u306b\u304f\u304b\u3063\u305f\u308a\u4e8b\u524d\u6e96\u5099\u304c\u82e5\u5e72\u9762\u5012\u3060\u3063\u305f\u308a\u3059\u308b\u304c\u3001\u4e0a\u306e\u3088\u3046\u306a\u624b\u9806\u306b\u5f93\u3048\u3070\u554f\u984c\u306a\u3044\u306f\u305a\u3002\u5206\u304b\u308a\u306b\u304f\u3044\u30dd\u30a4\u30f3\u30c8\u306f<\/p>\n<ul>\n<li>p12\u30ad\u30fc\u3092\u4f7f\u3046<\/li>\n<li>\u300c\u30b5\u30fc\u30d3\u30b9\u30a2\u30ab\u30a6\u30f3\u30c8\u300d\u3092\u4f7f\u3046<\/li>\n<li>\u305d\u306e\u30a2\u30ab\u30a6\u30f3\u30c8\u306b\u5bfe\u3057\u3066\u30b9\u30d7\u30ec\u30c3\u30c9\u30b7\u30fc\u30c8\u306e\u6a29\u9650\u3092\u4e0e\u3048\u308b<\/li>\n<\/ul>\n<p>\u3068\u3044\u3063\u305f\u3068\u3053\u308d\u304b\u3002<\/p>\n<p>\u306a\u304a\u3001Google API\u30b3\u30f3\u30bd\u30fc\u30eb\u3084API\u306e\u4ed5\u69d8\u306f\u7d50\u69cb\u5909\u308f\u308a\u3084\u3059\u3044\u306e\u3067\u3001\u305d\u306e\u6642\u70b9\u3067\u306e\u6700\u65b0\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3092\u53c2\u7167\u3059\u308b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u540c\u3058\u3053\u3068\u3084\u308b\u306e2\u5ea6\u76ee\u306a\u306e\u306b\u3001\u3059\u3063\u304b\u308a\u5fd8\u308c\u3066\u3057\u307e\u3063\u3066\u3044\u305f\u306e\u3067\u30013\u5ea6\u3081\u306b\u5099\u3048\u3066\u30e1\u30e2\u3002 \u3084\u308a\u305f\u3044\u3053\u3068 \uff08MySQL\u306b\u5165\u3063\u3066\u3044\u308b\uff09\u3042\u308b\u30c7\u30fc\u30bf\u3092\u3001Google Spreadsheets API\u3092\u4f7f\u3063\u3066\u30b9\u30d7\u30ec\u30c3\u30c9\u30b7\u30fc\u30c8\u306b\u66f8\u304d\u8fbc\u3080\u3002 &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[401],"tags":[939,938,956],"class_list":["post-1505","post","type-post","status-publish","format-standard","hentry","category-php","tag-google-api","tag-google-spreadsheets-api","tag-php"],"_links":{"self":[{"href":"https:\/\/kazu.tv\/blog\/wp-json\/wp\/v2\/posts\/1505","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kazu.tv\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kazu.tv\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kazu.tv\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kazu.tv\/blog\/wp-json\/wp\/v2\/comments?post=1505"}],"version-history":[{"count":3,"href":"https:\/\/kazu.tv\/blog\/wp-json\/wp\/v2\/posts\/1505\/revisions"}],"predecessor-version":[{"id":1508,"href":"https:\/\/kazu.tv\/blog\/wp-json\/wp\/v2\/posts\/1505\/revisions\/1508"}],"wp:attachment":[{"href":"https:\/\/kazu.tv\/blog\/wp-json\/wp\/v2\/media?parent=1505"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kazu.tv\/blog\/wp-json\/wp\/v2\/categories?post=1505"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kazu.tv\/blog\/wp-json\/wp\/v2\/tags?post=1505"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}