google api usage - no composer -
facts; shared server, php 5.6, linux not windows. apache.
trying achieve; google login plus google analytics data retrieval; without using composer
hurdle; gooogle docs tutorial doesnt work!
rant; stackoverflow's 2 link policy stupid. driving me crazy. had google login on previous site, easy. come again new site , it's changed. firstly, no options install components log in google, have upload 5000 src files 99% wont used! follow ( https:// developers.google.com/analytics/devguides/reporting/core/v3/quickstart/web-php ) google's exact instrutions tee , error.
"fatal error: class 'google_client' not found in..."
downloaded src linked googles docs; file : google-api-php-client-2.2.0_php54.zip https://github.com/google/google-api-php-client/releases linked https://github.com/google/google-api-php-client#download-the-release
extracted , uploaded in structure; home/google/ (all supplied extracted files) home/google/src/ (all supplied extracted files) home/google/vendor/ (all supplied extracted files)
used exact snippet google's tutorial;
index.php
// load google api php client library. require_once __dir__ . '/vendor/autoload.php'; // start session persist credentials. session_start(); // create client object , set authorization configuration // client_secretes.json downloaded developer console. $client = new google_client(); $client->setauthconfig(__dir__ . '/client_secrets.json'); $client->addscope(google_service_analytics::analytics_readonly); // if user has authorized app access token // else redirect ask user authorize access google analytics. if (isset($_session['access_token']) && $_session['access_token']) { // set access token on client. $client->setaccesstoken($_session['access_token']); // create authorized analytics service object. $analytics = new google_service_analytics($client); // first view (profile) id authorized user. $profile = getfirstprofileid($analytics); // results core reporting api , print results. $results = getresults($analytics, $profile); printresults($results); } else { $redirect_uri = 'http://' . $_server['http_host'] . '/oauth2callback.php'; header('location: ' . filter_var($redirect_uri, filter_sanitize_url)); } function getfirstprofileid($analytics) { // user's first view (profile) id. // list of accounts authorized user. $accounts = $analytics->management_accounts->listmanagementaccounts(); if (count($accounts->getitems()) > 0) { $items = $accounts->getitems(); $firstaccountid = $items[0]->getid(); // list of properties authorized user. $properties = $analytics->management_webproperties ->listmanagementwebproperties($firstaccountid); if (count($properties->getitems()) > 0) { $items = $properties->getitems(); $firstpropertyid = $items[0]->getid(); // list of views (profiles) authorized user. $profiles = $analytics->management_profiles ->listmanagementprofiles($firstaccountid, $firstpropertyid); if (count($profiles->getitems()) > 0) { $items = $profiles->getitems(); // return first view (profile) id. return $items[0]->getid(); } else { throw new exception('no views (profiles) found user.'); } } else { throw new exception('no properties found user.'); } } else { throw new exception('no accounts found user.'); } } function getresults($analytics, $profileid) { // calls core reporting api , queries number of sessions // last 7 days. return $analytics->data_ga->get( 'ga:' . $profileid, '7daysago', 'today', 'ga:sessions'); } function printresults($results) { // parses response core reporting api , prints // profile name , total sessions. if (count($results->getrows()) > 0) { // profile name. $profilename = $results->getprofileinfo()->getprofilename(); // entry first entry in first row. $rows = $results->getrows(); $sessions = $rows[0][0]; // print results. print "<p>first view (profile) found: $profilename</p>"; print "<p>total sessions: $sessions</p>"; } else { print "<p>no results found.</p>"; } }
oauth2callback.php
// load google api php client library. require_once __dir__ . '/vendor/autoload.php'; // start session persist credentials. session_start(); // create client object , set authorization configuration // client_secrets.json downloaded developers console. $client = new google_client(); $client->setauthconfig(__dir__ . '/client_secrets.json'); $client->setredirecturi('http://' . $_server['http_host'] . '/oauth2callback.php'); $client->addscope(google_service_analytics::analytics_readonly); // handle authorization flow server. if (! isset($_get['code'])) { $auth_url = $client->createauthurl(); header('location: ' . filter_var($auth_url, filter_sanitize_url)); } else { $client->authenticate($_get['code']); $_session['access_token'] = $client->getaccesstoken(); $redirect_uri = 'http://' . $_server['http_host'] . '/'; header('location: ' . filter_var($redirect_uri, filter_sanitize_url)); }
2 days wasted, pissing around different api src files , demo tutorials.
put me out of misery please somebody.
thanks in advance.
Comments
Post a Comment