Skip to content

Instantly share code, notes, and snippets.

@sosroInSpace
Created February 18, 2021 08:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sosroInSpace/d5326d3a04924f3d1972e98b96ce54a4 to your computer and use it in GitHub Desktop.
Save sosroInSpace/d5326d3a04924f3d1972e98b96ce54a4 to your computer and use it in GitHub Desktop.
send data from wordpress to google sheet via php
<?php
// TODO: validate input params, ignore bots...
require_once("../../../../wp-load.php");
require __DIR__ . '/vendor/autoload.php'; // google-api-php-client path
$plugin_list = get_plugins();
$output = "";
foreach($plugin_list as $item) {
$item = json_decode(json_encode($item), true);
$name = $item["Name"];
$version = $item["Version"];
$output .= $name . " -- Version: " . $version . "\n";
}
echo $output;
function getClient()
{
$client = new Google_Client();
$client->setApplicationName('Project');
$client->setScopes(Google_Service_Sheets::SPREADSHEETS);
//PATH TO JSON FILE DOWNLOADED FROM GOOGLE CONSOLE FROM STEP 7
$client->setAuthConfig('credentials.json');
$client->setAccessType('offline');
return $client;
}
// Get the API client and construct the service object.
$client = getClient();
$service = new Google_Service_Sheets($client);
$spreadsheetId = '18IBvZRe2XVPlQ3-Na_f9I2SmsknLXxtZvAoCkF8GZ8s'; // spreadsheet Id
$range = 'okmg.com'; // Sheet name
$valueRange= new Google_Service_Sheets_ValueRange();
$valueRange->setValues(["values" => ["a", "b"]]); // values for each cell
$valueRange->setValues(["values" => [
$output
, date("F j, Y, g:i a", time())
]]);
$conf = ["valueInputOption" => "RAW"];
$response = $service->spreadsheets_values->append($spreadsheetId, $range, $valueRange, $conf);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment