简体   繁体   English

通过PHP连接到Google AdSense API

[英]Connect to Google AdSense API via PHP

I'm trying to connect to AdSense API using a PHP script. 我正在尝试使用PHP脚本连接到AdSense API。 I started using this tutorial by google: https://developers.google.com/api-client-library/php/start/get_started#build-the-client-object 我开始使用Google制作的本教程: https : //developers.google.com/api-client-library/php/start/get_started#build-the-client-object

However, I didn't manage to connect. 但是,我没有连接。 This is what I've tried: 这是我尝试过的:

$client = new Google_Client();
$client->setApplicationName("AppName");
$client->setDeveloperKey(API_Key);
$client->setAuthConfigFile('../AdSense/google-api-php-client/client_secret.json');

$service = new Google_Service_AdSenseTest($client);
$results = $service->testReportsGenerate();

foreach($results as $item)
{
    echo $item;
}

And I came across a few problems, the main one is that the code doesn't recognize the "Google_Service_AdSenseTest" class - even though the code suggested it. 我遇到了一些问题,主要的问题是该代码无法识别“ Google_Service_AdSenseTest”类,即使该代码提出了建议也是如此。 So my real question is this: What service should I use if I want to pull data from AdSense? 所以我真正的问题是:如果我想从AdSense中提取数据,应该使用哪种服务? And how do I set the needed parameters (meaning - which dimensions and metrics to get)? 以及如何设置所需的参数(含义-获取哪些维度和指标)?

Thank you. 谢谢。

You need to implement OAuth requests as you do for every single google api, (use Phil Sturgeon's OAuth2.0 protocol if you use codeigniter, well implemented.) or any oauth2 client scripts will do. 您需要像对每个单独的Google api一样实施OAuth请求(如果使用良好的Codeigniter,请使用Phil Sturgeon的OAuth2.0协议,如果实施得当),或者任何oauth2客户端脚本都可以。

The google api library is here : https://github.com/googleapis/google-api-php-client-services . google api库位于: https : //github.com/googleapis/google-api-php-client-services Use composer to install these libraries. 使用composer安装这些库。

The adsense class is here : https://github.com/googleapis/google-api-php-client-services/blob/master/src/Google/Service/AdSense.php adsense类位于此处: https : //github.com/googleapis/google-api-php-client-services/blob/master/src/Google/Service/AdSense.php

The class/function which makes the request to google must setup an oauth client and specify a redirect url (which in turn must be registered on google api console ) 向Google发出请求的类/函数必须设置一个oauth客户端并指定重定向网址(该网址必须在google api控制台上注册)

The scopes are 范围是
../auth/adsense ../auth/adsense.readonly ../auth/adsense ../auth/adsense.readonly

Once everything is done, you can make a request. 完成所有操作后,您可以提出请求。

I implemented it with Codeigniter for searchconsole, adsense and other useful libraries they all works awesome. 我用Codeigniter实现了它的搜索控制台,adsense和其他有用的库,它们都很棒。 Besides i connected google sheets as well, so every report is available to me in google sheet as it is needed. 此外,我还连接了Google表格,因此每个报表都可以根据需要在Google表格中提供给我。

With oAuth Access token, The code : 使用oAuth访问令牌,代码:

$client = new Google_Client();
$adsenseService = new Google_Service_AdSense(...);
$client->setApplicationName("Adsense Console");
                        $client->setDeveloperKey($apiKey);
                        $client->setAccessToken( $token->access_token );

$params = array('maxResults' => 1000, 'pageToken' => null, 'alt' => 'json', 'fields' => array(), 'prettyPrint' => true, 'quotaUser' => '', 'userIp' => '' );

$accounts = $adsenseService->accounts->listAccounts($params);
//this will print a json array 
echo '<pre>' ; print_r($accounts); echo '<pre>'; die();

for adclients, 对于广告客户,

$adsense->adclients->listAccountsAdclients($params);

The params ref is here, https://developers.google.com/adsense/management/v1.4/reference/accounts/adclients/list#try-it 参数引用在这里https://developers.google.com/adsense/management/v1.4/reference/accounts/adclients/list#try-it

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM