function writeGoogleSpreadsheet($data)
{
    if (empty($data)) {
        return FALSE;
    }
    session_start();
    include_once "google-api-php-client/examples/templates/base.php";
    require_once realpath(dirname(__FILE__) . '/google-api-php-client/src/Google/autoload.php');
    $client_id = '76948799826-4u3c0b83kqcas2j034rivdsp2ik8qjp5.apps.googleusercontent.com';
    $service_account_name = '*****@*****.**';
    $key_file_location = 'FeedBackMail-de82c85c3106.p12';
    if (strpos($client_id, "googleusercontent") == false || !strlen($service_account_name) || !strlen($key_file_location)) {
        echo missingServiceAccountDetailsWarning();
        exit;
    }
    $client = new Google_Client();
    $client->setApplicationName("FeedBackMail");
    $service = new Google_Service_Drive($client);
    if (isset($_SESSION['service_token'])) {
        $client->setAccessToken($_SESSION['service_token']);
    }
    $key = file_get_contents($key_file_location);
    $cred = new Google_Auth_AssertionCredentials($service_account_name, array('https://www.googleapis.com/auth/drive', 'https://spreadsheets.google.com/feeds'), $key);
    $client->setAssertionCredentials($cred);
    if ($client->getAuth()->isAccessTokenExpired()) {
        $client->getAuth()->refreshTokenWithAssertion($cred);
    }
    $_SESSION['service_token'] = $client->getAccessToken();
    $resultArray = json_decode($_SESSION['service_token']);
    $accessToken = $resultArray->access_token;
    $fileId = '1vlMTld652YHY0ey3NshxKjo7WriaGsv31dMpS8Fhp30';
    $url = "https://spreadsheets.google.com/feeds/list/{$fileId}/od6/private/full";
    $method = 'POST';
    $headers = ["Authorization" => "Bearer {$accessToken}", 'Content-Type' => 'application/atom+xml'];
    $postBody = '<entry xmlns="http://www.w3.org/2005/Atom" xmlns:gsx="http://schemas.google.com/spreadsheets/2006/extended">';
    $postBody .= '<gsx:created>' . $data['date_created'] . '</gsx:created>';
    $postBody .= '<gsx:fio>' . $data['fio'] . '</gsx:fio>';
    $postBody .= '<gsx:email>' . $data['email'] . '</gsx:email>';
    $postBody .= '<gsx:message>' . $data['message'] . '</gsx:message>';
    $postBody .= '</entry>';
    $req = new Google_Http_Request($url, $method, $headers, $postBody);
    $curl = new Google_IO_Curl($client);
    try {
        $curl->executeRequest($req);
        return TRUE;
    } catch (Exception $e) {
        return FALSE;
    }
}
Example #2
0
function get_worksheets($ce, $id)
{
    $url = "https://spreadsheets.google.com/feeds/worksheets/{$id}/private/full";
    $method = 'GET';
    $headers = ["Authorization" => "Bearer {$ce->auth->access_token}"];
    $req = new Google_Http_Request($url, $method, $headers);
    $curl = new Google_IO_Curl($ce->client);
    $results = $curl->executeRequest($req);
    //    echo "$results[2]\n\n";
    //    echo "$results[0]\n";
    return simplexml_load_string($results[0]);
}