<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
michaelcrowcroft / google-search-console-laravel example snippets
use GoogleSearchConsole;
// Set your access token and default site URL
GoogleSearchConsole::setAccessToken($token);
GoogleSearchConsole::setSiteUrl('https://example.com');
// Query search analytics
$data = GoogleSearchConsole::analytics()->query([
'start_date' => '2024-01-01',
'end_date' => '2024-01-31',
'dimensions' => ['query', 'page'],
'row_limit' => 100
]);
// Helper methods
$queries = GoogleSearchConsole::analytics()->getQueryData('2024-01-01', '2024-01-31');
$pages = GoogleSearchConsole::analytics()->getPageData('2024-01-01', '2024-01-31');
$countries = GoogleSearchConsole::analytics()->getCountryData('2024-01-01', '2024-01-31');
// Pass a different site if you want to overwrite the set URL, or haven't set one already.
$queries = GoogleSearchConsole::analytics()->getQueryData('https://othersite.com', '2024-01-01', '2024-01-31');
// Get top queries (uses default site URL)
$queries = GoogleSearchConsole::analytics()->getQueryData('2024-01-01', '2024-01-31', 100);
// Get top pages
$pages = GoogleSearchConsole::analytics()->getPageData('2024-01-01', '2024-01-31', 100);
// Get country performance
$countries = GoogleSearchConsole::analytics()->getCountryData('2024-01-01', '2024-01-31', 50);
// Get device performance
$devices = GoogleSearchConsole::analytics()->getDeviceData('2024-01-01', '2024-01-31');
// Get query and page combinations
$queryPages = GoogleSearchConsole::analytics()->getQueryPageData('2024-01-01', '2024-01-31');
// Or specify different site URL for specific calls
$queries = GoogleSearchConsole::analytics()->getQueryData('https://othersite.com', '2024-01-01', '2024-01-31');
// List sitemaps (uses default site URL)
$sitemaps = GoogleSearchConsole::sitemaps()->list();
// Submit sitemap
GoogleSearchConsole::sitemaps()->submit('sitemap.xml');
// Get sitemap details
$sitemap = GoogleSearchConsole::sitemaps()->get('sitemap.xml');
// Delete sitemap
GoogleSearchConsole::sitemaps()->delete('old-sitemap.xml');
// Or specify different site URL
$sitemaps = GoogleSearchConsole::sitemaps()->list('https://othersite.com');
// List all verified sites
$sites = GoogleSearchConsole::sites()->list();
// Get site details (uses default site URL)
$site = GoogleSearchConsole::sites()->get();
// Check verification
$isVerified = GoogleSearchConsole::sites()->isVerified();
// Or specify different site URL
$site = GoogleSearchConsole::sites()->get('https://othersite.com');
// Inspect URL (uses default site URL)
$result = GoogleSearchConsole::urlInspection()->inspect('https://example.com/page');
// Check if indexed
$isIndexed = GoogleSearchConsole::urlInspection()->isIndexed('https://example.com/page');
// Or specify different site URL
$result = GoogleSearchConsole::urlInspection()->inspect('https://othersite.com', 'https://othersite.com/page');
// Set access token
GoogleSearchConsole::setAccessToken($token);
// Set default site URL
GoogleSearchConsole::setSiteUrl('https://example.com');
// Check validity
if (GoogleSearchConsole::isAccessTokenValid()) {
// Proceed
}
// Get current values
$token = GoogleSearchConsole::getAccessToken();
$siteUrl = GoogleSearchConsole::getSiteUrl();