samiaraboglu/google-api-client-php-bundle
最新稳定版本:0.3.1
Composer 安装命令:
composer require samiaraboglu/google-api-client-php-bundle
包简介
Symfony Google Api Client Bundle
README 文档
README
Use the Google APIs Client Library for PHP.
Download the Bundle
$ composer require samiaraboglu/google-api-client-php-bundle
Enable the Bundle
Registered bundles in the app/AppKernel.php file of your project:
<?php class AppKernel extends Kernel { public function registerBundles() { $bundles = array( // ... new Samiax\GoogleApiBundle\SamiaxGoogleApiBundle(), ); // ... } // ... }
Config
Add this to config.yml:
samiax_google_api: credential_file: "%kernel.root_dir%/config/google-api-client-php/client_credentials.json" application_name: "APPLICATION_NAME"
Example 1 - Google Analytics
Get the session count from google analytics.
/** * @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(); }
Example 2 - Google Product Feed
Get the product feeds.
/** * @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(); }
统计信息
- 总下载量: 20.04k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-07-09