谷歌API客户端和嵌套的客户端需要?

时间:2014-06-11 20:42:29

标签: wordpress google-api-php-client

我正在尝试在wordpress仪表板插件中实施Google analytics API。

首次使用Google API for PHP的最简单实现,它几乎不会立即开始工作。

根据教程README.md,我的应用程序位于库文件夹的旁边,应该像这样加载库:

require_once 'Google/Client.php';
require_once 'Google/Service/Books.php';

$client = new Google_Client();

现在,我的应用程序(插件)中的库结构是:

myapp.php
/Google
/Google/Client.php
/Google/otherfolders/otherfiles.php

我的应用尝试按照上面的require_once加载库。但当然Client.php本身有许多require_once调用,例如:

require_once 'Google/Auth/AssertionCredentials.php';

似乎忽略了它的位置 - 已经在/ Google中。

所以我收到了错误:

PHP Warning: require_once(Google/Auth/AssertionCredentials.php) [<a href='function.require-once'>function.require-once</a>]: failed to open stream: No such file or directory in <wpengine host path removed>/wp-content/plugins/mmstats/Google/Client.php on line 18

PHP Fatal error: require_once() [<a href='function.require'>function.require</a>]: Failed opening required 'Google/Auth/AssertionCredentials.php' (include_path='.:/usr/share/php:/usr/share/pear') in <wpengine host path removed>/wp-content/plugins/mmstats/Google/Client.php on line 18

这个PHP被列为“Beta”但肯定我做错了,而不是Client.php的问题

任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:0)

Google Director需要与您的文件位于同一目录中。如果您不想自己编辑所有required_once。以下是一些用于Google AnalyticsAPI的工作代码。

<?php         
require_once 'Google/Client.php';     
require_once 'Google/Service/Analytics.php';       
session_start();      
$client = new Google_Client();
    $client->setApplicationName("Client_Library_Examples");
    $client->setDeveloperKey("{devkey}");  
    $client->setClientId('{clientid}.apps.googleusercontent.com');
    $client->setClientSecret('{clientsecret}');
    $client->setRedirectUri('http://www.daimto.com/Tutorials/PHP/Oauth2.php');
    $client->setScopes(array('https://www.googleapis.com/auth/analytics.readonly'));

    //For loging out.
    if ($_GET['logout'] == "1") {
    unset($_SESSION['token']);
       }   

    // Step 2: The user accepted your access now you need to exchange it.
    if (isset($_GET['code'])) {

        $client->authenticate($_GET['code']);  
        $_SESSION['token'] = $client->getAccessToken();
        $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
        header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
    }

    // Step 1:  The user has not authenticated we give them a link to login    
    if (!$client->getAccessToken() && !isset($_SESSION['token'])) {
        $authUrl = $client->createAuthUrl();
        print "<a class='login' href='$authUrl'>Connect Me!</a>";
        }        

    // Step 3: We have access we can now create our service
    if (isset($_SESSION['token'])) {
        print "<a class='logout' href='".$_SERVER['PHP_SELF']."?logout=1'>LogOut</a><br>";
        $client->setAccessToken($_SESSION['token']);
        $service = new Google_Service_Analytics($client);    

        // request user accounts
        $accounts = $service->management_accountSummaries->listManagementAccountSummaries();

       foreach ($accounts->getItems() as $item) {
        echo "Account: ",$item['name'], "  " , $item['id'], "<br /> \n";        
        foreach($item->getWebProperties() as $wp) {
            echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WebProperty: ' ,$wp['name'], "  " , $wp['id'], "<br /> \n";    

            $views = $wp->getProfiles();
            if (!is_null($views)) {
                foreach($wp->getProfiles() as $view) {
                //  echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;View: ' ,$view['name'], "  " , $view['id'], "<br /> \n";    
                }
            }
        }
    } // closes account summaries

    }
 print "<br><br><br>";
 print "Access from google: " . $_SESSION['token']; 
?>

您可以在Google Oauth2 PHP找到此代码的教程,底部有一个小测试应用,您可以看到它正常工作。

相关问题