PHP code example of samiaraboglu / google-api-client-php-bundle
1. Go to this page and download the library: Download samiaraboglu/google-api-client-php-bundle library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
samiaraboglu / google-api-client-php-bundle example snippets
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
// ...
new Samiax\GoogleApiBundle\SamiaxGoogleApiBundle(),
);
// ...
}
// ...
}
/**
* @Route("/google/analytics", name="google_analytics")
*/
public function googleAnalyticsAction(Request $request)
{
$service = $this->get('samiax_google_api.google_client');
$googleClient = $service->getGoogleClient();
$googleClient->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);
$analytics = $service->analytics();
// Create the DateRange object.
$dateRange = new \Google_Service_AnalyticsReporting_DateRange();
$dateRange->setStartDate("1daysAgo");
$dateRange->setEndDate("1daysAgo");
// Create the Metrics object.
$sessions = new \Google_Service_AnalyticsReporting_Metric();
$sessions->setExpression("ga:sessions");
$sessions->setAlias("sessions");
// Create the ReportRequest object.
$request = new \Google_Service_AnalyticsReporting_ReportRequest();
$request->setViewId("{VIEW_ID}");
$request->setDateRanges($dateRange);
$request->setMetrics([$sessions]);
$body = new \Google_Service_AnalyticsReporting_GetReportsRequest();
$body->setReportRequests([$request]);
echo $analytics->reports->batchGet($body)->getReports()[0]->getData()->getTotals()[0]->getValues()[0];
return new Response();
}
/**
* @Route("/google/content/auth", name="google_content_auth")
*/
public function googleContentAuthAction(Request $request)
{
$service = $this->get('samiax_google_api.google_client');
$googleClient = $service->getGoogleClient();
$googleClient->setRedirectUri($request->getSchemeAndHttpHost() . $request->getBaseUrl() . $request->getPathInfo());
$googleClient->setScopes('https://www.googleapis.com/auth/content');
if ($request->query->get('code')) {
$googleClient->authenticate($request->query->get('code'));
$session = $this->container->get('session');
$session->set('google_content_access_token', $googleClient->getAccessToken());
return $this->redirect(filter_var($this->generateUrl('google_content'), FILTER_SANITIZE_URL));
}
return $this->redirect($googleClient->createAuthUrl());
}
/**
* @Route("/google/content", name="google_content")
*/
public function googleContentAction()
{
$session = $this->container->get('session');
$accessToken = $session->get('google_content_access_token');
if (!$accessToken) {
return $this->redirect($this->generateUrl('google_content_auth'));
}
$service = $this->get('samiax_google_api.google_client');
$googleClient = $service->getGoogleClient();
$googleClient->setAccessToken($accessToken);
$shoppingContent = $service->shoppingContent();
$products = $shoppingContent->products->listProducts({MERCHANT_ID});
var_dump($products);
return new Response();
}