https://github.com/samiaraboglu/google-api-client-php-bundle
Symfony Google Api Bundle
https://github.com/samiaraboglu/google-api-client-php-bundle
api google google-analytics google-api symfony symfony-bundle
Last synced: about 2 months ago
JSON representation
Symfony Google Api Bundle
- Host: GitHub
- URL: https://github.com/samiaraboglu/google-api-client-php-bundle
- Owner: samiaraboglu
- License: mit
- Created: 2018-07-09T07:40:34.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2019-08-03T12:48:00.000Z (almost 7 years ago)
- Last Synced: 2025-11-18T17:22:04.718Z (7 months ago)
- Topics: api, google, google-analytics, google-api, symfony, symfony-bundle
- Language: PHP
- Homepage:
- Size: 14.6 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# google-api-client-php-bundle
[](https://packagist.org/packages/samiaraboglu/google-api-client-php-bundle)
[](https://packagist.org/packages/samiaraboglu/google-api-client-php-bundle)
[](https://packagist.org/packages/samiaraboglu/google-api-client-php-bundle)
Use the [Google APIs Client Library for PHP](https://github.com/google/google-api-php-client).
### Download the Bundle
```console
$ composer require samiaraboglu/google-api-client-php-bundle
```
### Enable the Bundle
Registered bundles in the `app/AppKernel.php` file of your project:
```php
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();
}
```
### Example 2 - Google Product Feed
Get the product feeds.
```php
/**
* @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();
}
```