schulzefelix / laravel-search-console

一个用于从Google Search Console检索数据的Laravel包

1.8.0 2022-03-11 16:00 UTC

This package is auto-updated.

Last update: 2024-09-11 21:23:58 UTC


README

Latest Version Software License Build Status Quality Score StyleCI Latest Version on Packagist Total Downloads

使用此包,您可以轻松地从Google Search Console API检索数据。

以下是提供的一些方法示例

use SearchConsole;

//list all available sites for that token
SearchConsole::setAccessToken($token)->listSites();

//get site details (permissionLevel) for specific site
SearchConsole::setAccessToken($token)->getSite('http://blog.example.com/');

安装

此包可以通过Composer安装。

$ composer require schulzefelix/laravel-search-console

可选地,您可以使用以下命令发布此包的配置文件

php artisan vendor:publish --provider="SchulzeFelix\SearchConsole\SearchConsoleServiceProvider"

以下配置文件将发布到config/search-console.php

return [

    /*
    |--------------------------------------------------------------------------
    | Authentication
    |--------------------------------------------------------------------------
    | Google offers access via OAuth client IDs or service accounts.
    | For more information see: https://developers.google.com/identity/protocols/OAuth2
    |
    | Supported: "oauth", "oauth_json", "service_account",
    */
 
    'auth_type' => env('GOOGLE_AUTH_TYPE', 'oauth'),
 
    /*
    |--------------------------------------------------------------------------
    | Application Credentials
    |--------------------------------------------------------------------------
    |
    | https://developers.google.com/api-client-library/php/auth/service-accounts#creatinganaccount
    */
 
    'connections' => [
 
        'oauth' => [
            'client_id' => env('GOOGLE_CLIENT_ID'),
            'client_secret' => env('GOOGLE_CLIENT_SECRET'),
        ],
 
        'oauth_json' => [
            'auth_config' => storage_path('app/searchconsole/oauth-account-credentials.json'),
        ],
 
        'service_account' => [
            'application_credentials' => storage_path('app/searchconsole/service-account-credentials.json'),
        ],
 
    ],
 
    /*
     |--------------------------------------------------------------------------
     | Cache Settings
     |--------------------------------------------------------------------------
     | Here you may configure the "store" that the underlying Google_Client will
     | use to store it's data.  You may also add extra parameters that will
     | be passed on setCacheConfig (see docs for google-api-php-client).
     |
     | Optional parameters: "lifetime", "prefix"
     */
 
    'cache' => [
        'store' => 'file',
    ],
 
    /*
    |--------------------------------------------------------------------------
    | Application Name
    |--------------------------------------------------------------------------
    */
 
    'application_name' => env('SEARCH_CONSOLE_APPLICATION_NAME', 'GSC Agent'),
];

用法

以下是两个基本示例:检索所有站点和导出搜索分析数据。

列出站点

$sites = SearchConsole::setAccessToken($token)->listSites();

搜索分析

use SearchConsole;
use SchulzeFelix\SearchConsole\Period;

    $data = SearchConsole::setAccessToken($token)->setQuotaUser('uniqueQuotaUserString')
        ->searchAnalyticsQuery(
            'https://www.example.com/',
            Period::create(Carbon::now()->subDays(30), Carbon::now()->subDays(2)),
            ['query', 'page', 'country', 'device', 'date'],
            [['dimension' => 'query', 'operator' => 'notContains', 'expression' => 'cheesecake']],
            1000,
            'web',
            'all',
            'auto'
        );

提供的方法

检索单个站点

public function public function getSite(string $siteUrl): array

检索所有站点

public function public function listSites(): Collection

检索搜索分析数据

public function searchAnalyticsQuery(string $siteUrl, Period $period, array $dimensions = [], array $filters = [], int $rows = 1000, string $searchType = 'web', string $dataState = 'final', string $aggregationType = 'auto'): Collection

检查访问令牌

public function public function isAccessTokenExpired(): Bool

提供的流畅配置

设置访问令牌(必需)

$sites = SearchConsole::setAccessToken($token)->listSites();

设置配额用户

为了避免API限制,您可以提供一个唯一的字符串用于认证账户。

更多信息:https://developers.google.com/webmaster-tools/search-console-api-original/v3/limits

$sites = SearchConsole::setAccessToken($token)->setQuotaUser('uniqueQuotaUserString')->listSites();

获取底层服务

您可以访问底层的Google_Service_Webmasters对象

SearchConsole::getWebmastersService();

变更日志

请参阅CHANGELOG以获取更多关于最近更改的信息。

测试

$ vendor/bin/phpunit

贡献

请参阅CONTRIBUTINGCONDUCT以获取详细信息。

安全性

如果您发现任何与安全相关的问题,请通过电子邮件github@schulze.co联系,而不是使用问题跟踪器。

致谢

许可

MIT许可证(MIT)。有关更多信息,请参阅许可文件