zoidberg-ufo/laravel-search-console

一个用于从Google Search Console获取数据的Laravel扩展包

v1.8.1 2024-03-27 15:11 UTC

This package is auto-updated.

Last update: 2024-09-27 16:23:35 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)。请参阅 许可证文件 以获取更多信息。