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 = '76948799826-8t5kei17nghfdrkovgaifpdridouteu5@developer.gserviceaccount.com';
    $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
 /**
  * Creates a new YouTube instance
  *
  * @return void
  */
 public function __construct()
 {
     include_once 'GoogleSDK/src/Google/autoload.php';
     $this->_appId = 'Helioviewer.org User Video Uploader';
     $this->_clientId = 'Helioviewer.org (2.4.0)';
     //$this->_testURL   = 'http://gdata.youtube.com/feeds/api/users/default/uploads?max-results=1';
     //$this->_uploadURL = 'http://uploads.gdata.youtube.com/feeds/api/users/default/uploads';
     if (!isset($_SESSION)) {
         # Note: If same user begins upload, and then shortly after tried
         # to upload a video again, PHP may hang when attempting to call
         # session_start(). May need a better way to make sure nothing else
         # is going on? (e.g. check for video entry in YouTube)
         session_start();
     }
     $this->_httpClient = new Google_Client();
     $this->_httpClient->setApplicationName("Helioviewer");
     $this->_httpClient->setClientId(HV_GOOGLE_OAUTH2_CLIENT_ID);
     $this->_httpClient->setClientSecret(HV_GOOGLE_OAUTH2_CLIENT_SECRET);
     $this->_httpClient->setScopes('https://www.googleapis.com/auth/youtube.upload');
     // Post-auth upload URL
     $redirect = filter_var(HV_WEB_ROOT_URL . '?action=uploadMovieToYouTube&html=true', FILTER_SANITIZE_URL);
     $this->_httpClient->setRedirectUri($redirect);
     //set proxy config for curl io
     if (!empty(HV_PROXY_HOST)) {
         $io = new Google_IO_Curl($this->_httpClient);
         $curloptions = array();
         $curloptions[CURLOPT_PROXY] = HV_PROXY_HOST;
         if (!empty(HV_PROXY_USER_PASSWORD)) {
             $curloptions[CURLOPT_PROXYUSERPWD] = HV_PROXY_USER_PASSWORD;
         }
         $io->setOptions($curloptions);
         $this->_httpClient->setIo($io);
     }
 }
Example #3
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]);
}
 public function metadata()
 {
     $gcurl = new Google_IO_Curl($this->client);
     $response = $gcurl->makeRequest(new Google_Http_Request("https://www.googleapis.com/analytics/v3/metadata/ga/columns"));
     //verify returned data
     $data = json_decode($response->getResponseBody());
     $items = $data->items;
     $data_items = [];
     $dimensions_data = [];
     $metrics_data = [];
     foreach ($items as $item) {
         if ($item->attributes->status == 'DEPRECATED') {
             continue;
         }
         if ($item->attributes->type == 'DIMENSION') {
             $dimensions_data[$item->attributes->group][] = $item;
         }
         if ($item->attributes->type == 'METRIC') {
             $metrics_data[$item->attributes->group][] = $item;
         }
     }
     //foreach
     $data_items['dimensions'] = $dimensions_data;
     $data_items['metrics'] = $metrics_data;
     return $data_items;
 }