Skip to content

Instantly share code, notes, and snippets.

@sakadon
Last active November 5, 2015 18:59
Show Gist options
  • Save sakadon/b251728d4aa963d0c690 to your computer and use it in GitHub Desktop.
Save sakadon/b251728d4aa963d0c690 to your computer and use it in GitHub Desktop.
Google SpreadSheetsをGoogle APIs Client Library for PHP 1.1.6で叩くための準備 ref: http://qiita.com/sakadon/items/84593201b8801a8a3a8a
{
"private_key_id": "################",
"private_key": "-----BEGIN PRIVATE KEY-----\#####################################################################################################-----END PRIVATE KEY-----\n",
"client_email": "########@developer.gserviceaccount.com",
"client_id": "#########gsa.apps.googleusercontent.com",
"type": "service_account"
}
// load Google APIs Client Library for PHP ver 1.1.6
require_once "/unko/Google/autoload.php";
$client = new Google_Client();
$client->setApplicationName("getSpreadsheet");
$credentials = $client->loadServiceAccountJson("/unko/.secret/certkey.json", array(
Google_Service_Drive::DRIVE
));
$client->setAssertionCredentials($credentials);
$client->getAuth()->refreshTokenWithAssertion();
$accessTokenJson = $client->getAccessToken();
$accessToken = json_decode($accessTokenJson)->access_token;
// GET spreadsheets
$sheet_id = "##########################################";
$request = new Google_Http_Request(
"https://spreadsheets.google.com/feeds/worksheets/{$sheet_id}/private/full?alt=json",
"GET",
array("Authorization" => "Bearer {$accessToken}")
);
$curl = new Google_IO_Curl($client);
$res = $curl->executeRequest($request);
// データ
$data = json_decode($res[0]);
//print_r( $data['feed']['entry'] )
foreach ($data->feed->entry as $sheets) {
if( $sheets->title->{"\$t"} == 'シート名は直接指定' ) {
foreach ($sheets->link as $links) {
if( $links->rel == 'http://schemas.google.com/spreadsheets/2006#listfeed' ){
$listfeed = $links->href;
echo '$listfeed: '.$listfeed;
}
if( $links->rel == 'http://schemas.google.com/spreadsheets/2006#cellsfeed' ){
$cellsfeed = $links->href;
echo '$cellsfeed: '.$cellsfeed;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment