我仍然是任何API的初学者,所以需要帮助。据我所知,google-api-php-client库中的服务"Webmasters"允许我接收CTR
, Clicks
等数据
我从github下载了lib文件,并把它放在了localhost上。然后在谷歌开发者控制台我创建项目(不太明白,为什么?这个项目不包含关于网站的任何信息,其中搜索信息我需要)。然后为项目创建服务器密钥(通过"添加凭据"在Google Developers Console中,不输入任何ip)。Google Search Console API已启用。我是我的网站的完整用户(我可以看到它在谷歌搜索控制台)。我也有谷歌账号,当然,并且登录了。
在lib的examples文件夹中创建的源文件,以及其他示例:
include_once "templates/base.php";
require_once realpath(dirname(__FILE__) . '/../src/Google/autoload.php');
$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
$apiKey = "AIzaSyCOJ56353XByxh8rCpfgfhgfhZzopSLUe"; // Value of server key, that I created in for my project ().
if (strpos($apiKey, "<") !== false) {
echo missingApiKeyWarning();
exit;
}
$client->setDeveloperKey($apiKey);
//here are my efforts
$service = new Google_Service_Webmasters($client);
var_dump($service->searchanalytics->query(
'http://sschesnok.com.ua',
new Google_Service_Webmasters_SearchAnalyticsQueryRequest())); //I'm not sure about correctness of 2nd param
我看到错误:
<b>Fatal error</b>: Uncaught exception 'Google_Service_Exception' with message 'Error calling GET https://www.googleapis.com/webmasters/v3/sites?key=AIzaSyCOJXByxh8rCpfZzopSLUerl6LjWmziqhw: (401) Login Required' in G:'server'www'gwt'gs'src'Google'Http'REST.php:110
Stack trace:
#0 G:'server'www'gwt'gs'src'Google'Http'REST.php(62): Google_Http_REST::decodeHttpResponse(Object(Google_Http_Request), Object(Google_Client))
#1 [internal function]: Google_Http_REST::doExecute(Object(Google_Client), Object(Google_Http_Request))
#2 G:'server'www'gwt'gs'src'Google'Task'Runner.php(174): call_user_func_array(Array, Array)
#3 G:'server'www'gwt'gs'src'Google'Http'REST.php(46): Google_Task_Runner->run()
#4 G:'server'www'gwt'gs'src'Google'Client.php(593): Google_Http_REST::execute(Object(Google_Client), Object(Google_Http_Request))
#5 G:'server'www'gwt'gs'src'Google'Service'Resource.php(237): Google_Client->execute(Object(Google_Http_Request))
#6 G:'server'www'gwt'gs'src'Google'Service'Webmasters.php(492): Google_Service_Resource->call('list', A in <b>G:'server'www'gwt'gs'src'Google'Http'REST.php</b> on line <b>110</b><br />
(401) Login Required
-我错在哪里?我需要通过什么登录和在哪里?
第二个问题-我需要把什么作为第二个参数传递给query
方法?
请帮我弄清楚:从这个库中检索搜索信息。我从来没有使用过任何API,所以对它几乎一无所知。
这是我的工作代码。我使用的是dev-master版本的这个库。
include_once "templates/base.php";
session_start();
require_once realpath(dirname(__FILE__) . '/../src/Google/autoload.php');
$client_id = '**********************.apps.googleusercontent.com';
$client_secret = '*******************';
$redirect_uri = 'http://localhost/gwt/gr/examples/user-example.php';
$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->addScope("https://www.googleapis.com/auth/webmasters");
if (isset($_REQUEST['logout'])) {
unset($_SESSION['access_token']);
}
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
} else {
$authUrl = $client->createAuthUrl();
}
if ($client->getAccessToken()) {
$_SESSION['access_token'] = $client->getAccessToken();
$q = new 'Google_Service_Webmasters_SearchAnalyticsQueryRequest();
$q->setStartDate('2015-09-01');
$q->setEndDate('2015-09-01');
$q->setDimensions(['page']);
$q->setSearchType('web');
try {
$service = new Google_Service_Webmasters($client);
$u = $service->searchanalytics->query('http://sschesnok.com.ua', $q);
echo '<table border=1>';
echo '<tr>
<th>#</th><th>Clicks</th><th>CTR</th><th>Imp</th><th>Page</th><th>Avg. pos</th>';
for ($i = 0; $i < count($u->rows); $i++) {
echo "<tr><td>$i</td>";
echo "<td>{$u->rows[$i]->clicks}</td>";
echo "<td>{$u->rows[$i]->ctr}</td>";
echo "<td>{$u->rows[$i]->impressions}</td>";
echo "<td>{$u->rows[$i]->keys[0]}</td>";
echo "<td>{$u->rows[$i]->position}</td>";
/* foreach ($u->rows[$i] as $k => $value) {
//this loop does not work (?)
} */
echo "</tr>";
}
echo '</table>';
} catch('Exception $e ) {
echo $e->getMessage();
}
}
<div class="request">
<?php
if (isset($authUrl)) {
echo "<a class='login' href='" . $authUrl . "'>Connect Me!</a>";
} else {
echo <<<END
<form id="url" method="GET" action="{$_SERVER['PHP_SELF']}">
<input name="url" class="url" type="text">
<input type="submit" value="Shorten">
</form>
<a class='logout' href='?logout'>Logout</a>
END;
}
?>
</div>
访问令牌定期过期