-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-all-properties.php
68 lines (52 loc) · 1.98 KB
/
get-all-properties.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
declare(strict_types=1);
require_once dirname(__DIR__) . '/vendor/autoload.php';
use Abromeit\GscApiClient\GscApiClient;
use Google\Client;
use Google\Service\SearchConsole;
// Path to your service account JSON file
$serviceAccountFile = __DIR__ . '/../credentials/service-account.json';
if (!file_exists($serviceAccountFile)) {
$serviceAccountFile = realpath($serviceAccountFile);
die("Please provide your service account JSON file at: {$serviceAccountFile}\n");
}
try {
// Initialize Google Client with service account
$googleClient = new Client();
$googleClient->setAuthConfig($serviceAccountFile);
$googleClient->addScope(SearchConsole::WEBMASTERS_READONLY);
// Initialize our client
$client = new GscApiClient($googleClient);
// Get and display properties
$properties = $client->getProperties();
if (empty($properties)) {
echo "No properties found. Please make sure the service account has access to some properties.\n";
echo "\n";
exit(0);
}
// Display results
echo "Found " . count($properties) . " properties:\n";
echo "\n";
echo str_repeat('-', 80) . "\n";
echo sprintf("%-50s %s\n", 'Property URL', 'Permission Level');
echo str_repeat('-', 80) . "\n";
foreach ($properties as $property) {
echo sprintf(
"%-50s %s\n",
strlen($property->getSiteUrl()) > 49 ? mb_substr($property->getSiteUrl(), 0, 46) . '...' : $property->getSiteUrl(),
$property->getPermissionLevel()
);
}
echo str_repeat('-', 80) . "\n";
echo "\n";
// Example: Select the first property for further operations
if (isset($properties[0])) {
$firstProperty = $properties[0]->getSiteUrl();
echo "Selecting property: {$firstProperty}\n";
$client->setProperty($firstProperty);
echo "Current property: " . $client->getProperty() . "\n";
echo "\n";
}
} catch (Exception $e) {
die("Error: " . $e->getMessage() . "\n");
}