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.
Problem Solved.
I have created a form -
Then I used this POST method -
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.