Boo*_*ype 1 php api google-webmaster-tools google-api-php-client
我仍然是任何API的初学者,所以需要帮助.据我了解,服务"网站管理员"在谷歌的API的PHP客户端库允许我收到这样的数据CTR,Clicks等等.
我从github下载了lib文件并将其打入localhost.然后在谷歌开发者控制台中我创建了项目(真的不明白,为什么?这个项目不包含任何关于网站的信息,我需要哪些搜索信息).而对于项目创建服务器密钥后(通过"添加凭据"在谷歌开发者控制台,而无需输入任何IP它).Google Search Console API已启用.我是我网站的完全用户(我可以在Google Search Console中看到它).我也有谷歌帐户,当然,并登录.
我在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
Run Code Online (Sandbox Code Playgroud)
我看到错误:
<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 />
Run Code Online (Sandbox Code Playgroud)
(401) Login Required - 我哪里错了?什么登录以及我需要通过哪里?
第二个问题 - 我需要将第二个参数传递给query方法吗?
请帮我解决一下:通过这个lib检索搜索信息.我从未使用任何API,所以几乎什么都不懂.
这是我的工作代码.我使用了这个lib的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>
Run Code Online (Sandbox Code Playgroud)
访问令牌过期到期.
| 归档时间: |
|
| 查看次数: |
3437 次 |
| 最近记录: |