簡體   English   中英

如何將適用於Google API的PHP客戶端與Symfony 2集成在一起?

[英]How to integrate the PHP client for the Google API with Symfony 2?

我已經使用google-api-php-client創建了一個PHP應用程序。 我從Google控制台創建了一個Google帳戶電子郵件ID,從那里生成了P12文件,然后將其放入本地服務器中。 這對於“自定義” PHP可以正常工作。

現在,我想將google-api-php-client庫與我的Symfony項目集成到一個自定義包中。 我在/app/Resources內創建了一個文件夾“ LIB”,並將google-api-php-client所有文件都放置在那里。

然后, Google_Client放在主控制器內,以包括autoload.php文件並訪問Google_Client類:

//require_once($this->container->getParameter( 'kernel.root_dir' ). '/../src/ABC/Bundle/TTBundle/Lib/src/Google/autoload.php');
$service_account_email = 'xxxxx-yyyyyy@developer.gserviceaccount.com';
$key_file_location = 'API-Project.p12';

// Create and configure a new client object.
$client = new Google_Client();

但它顯示了以下錯誤:

FatalErrorException:錯誤:在D:\\ wamp \\ www \\ TTPR \\ current \\ src \\ ABC \\ Bundle \\ TTBundle \\ Controller \\ WebAnalyticsController.php行中找不到類'ABC \\ Bundle \\ TTBundle \\ Controller \\ Google_Client'

我通過以下步驟解決了該問題:

1-在我的控制器內創建一個函數:

2-在getService函數內部,我調用了我自己創建的函數以包含autoload.php文件:

需要知道的重要事項是放置“后退”(/),當創建班級對象時,我誤用了此“后退”並浪費了我的時間,希望這可以幫助您節省一些時間:)

protected function includeSsrsSdk()
    {
          require_once($this->container->getParameter( 'kernel.root_dir' ). '/../src/MWAN/Bundle/BIBundle/Lib/src/Google/autoload.php');

    }



public function getService()
    {


      $this->includeSsrsSdk();
      $service_account_email = 'xxyyzz@developer.gserviceaccount.com';
      $key_file_location = $this->container->getParameter( 'kernel.root_dir' ). '/../src/MWAN/Bundle/BIBundle/Lib/API-Project-xxxx.p12';

      // Create and configure a new client object.
      $client = new \Google_Client();
      $client->setApplicationName("HelloAnalytics");
      $analytics = new \Google_Service_Analytics($client);

      // Read the generated client_secrets.p12 key.
      $key = file_get_contents($key_file_location);
      $cred = new \Google_Auth_AssertionCredentials(
          $service_account_email,
          array(\Google_Service_Analytics::ANALYTICS_READONLY),
          $key
      );
      $client->setAssertionCredentials($cred);
      if($client->getAuth()->isAccessTokenExpired()) {
        $client->getAuth()->refreshTokenWithAssertion($cred);
      }

      return $analytics;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM