How to properly use Google Indexing API with PHP?

598 views Asked by At

I have configured Google Indexing API in my website. I am using raw PHP and I want to know how to use it properly?

This is the code from Google

require_once '../indexapi/vendor/autoload.php';

$client = new Google_Client();

$client->setAuthConfig('../indexapi/service_account_file.json');
$client->addScope('https://www.googleapis.com/auth/indexing');

$httpClient = $client->authorize();
$endpoint = 'https://indexing.googleapis.com/v3/urlNotifications:publish';

$content = '{
  "url": "mydomain.com/",
  "type": "URL_UPDATED"
}';

$response = $httpClient->post($endpoint, [ 'body' => $content ]);
$status_code = $response->getStatusCode();

Its working perfectly and I am getting status code = 200.

But what I want to know is - Is this the correct way to send multiple url for indexing?

$content = '{
  "url": "mydomain.com/","mydomain.com/page-1","mydomain.com/page-2",
  "type": "URL_UPDATED"
}';

or this?

$content = '{
  "url": "mydomain.com/",
  "type": "URL_UPDATED",
  "url": "mydomain.com/page-1",
  "type": "URL_UPDATED",
  "url": "mydomain.com/page-2",
  "type": "URL_UPDATED"
}';

Or, If I include this code in my website header and use this method -

$url = "https://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$content = '{
  "url": $url,
  "type": "URL_UPDATED"
  ----- OR
  "url": ".$url.", 
  "type": "URL_UPDATED"
  // Nothing treating this $url as a variable in my editor.
}';

Is it gonna work? Like, each time someone going to visit one of my website page, the code automatically generate a request to google to index it.

If the answer is yes, then I want to say one more thing, which is, the variable $url is not properly fitting inside $content variable. I have used this format ".$url." But Its not treating as a variable. Question here is, How to use variable in the "url": parameter?

Also I want to know, If I include the code into my header, then each time a user reload a page, is it going to send request to index the same url over and over again? Is it bad?

Overall, What I want to know is, How to use this segment of code (below) properly to index every url of my website? Also How to use variable inside this $content = '{ - }'

$content = '{
  "url": "mydomain.com",
  "type": "URL_UPDATED"
}';

Thanks in advance.

1

There are 1 answers

1
Bearded On BEST ANSWER

Problem Solved.

I have created a form -

<form action="" method="POST">
    <input type="text" name="url">
    <input type="hidden" name="type" value="URL_UPDATED">
    <button type="submit">Index Url</button>
</form>

Then I used this POST method -

require_once 'indexapi/vendor/autoload.php';

$client = new Google_Client();

if ($_SERVER['REQUEST_METHOD'] == "POST") {

    $url   = $_POST['url'];
    $type  = $_POST['type'];

    // Made an array with specific index name, what the api want!
    $array = ['url' => $url, 'type' => $type];

    // service_account_file.json, the private key created from service account.
    $client->setAuthConfig('indexapi/service_account_file.json');
    $client->addScope('https://www.googleapis.com/auth/indexing');

    // Get a Guzzle HTTP Client
    $httpClient = $client->authorize();
    $endpoint = 'https://indexing.googleapis.com/v3/urlNotifications:publish';

    // The contents
    // In here I json encoded the array I created earlier above.
    $content = json_encode($array);

    $response = $httpClient->post($endpoint, [ 'body' => $content ]);
    $status_code = $response->getStatusCode();

}

If you run this code and submit the form with an url in the input field, it will send a POST request to the API to index the url.

To see if its a successful submission, just check the status code if its returning 200 or not.

You can send single url indexing request each time you submit the form.