php [gsearch console] google搜索控制台api #google

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php [gsearch console] google搜索控制台api #google相关的知识,希望对你有一定的参考价值。

<?php
/**
https://developers.google.com/search/apis/indexing-api/v3/prereqs
+ enabling the Indexing API
+ creating a new service account (copy client_email in .json service account)
+ verifying ownership in Search Console (add above email: ie indexapi@project-id-3937556376642046437.iam.gserviceaccount.com  in Verified owners section for your site in google search console).
*/
require_once 'vendor/autoload.php';

$client = new Google_Client();
$client->setApplicationName("indexapi");
$client->setAuthConfig(__DIR__.'/google-index-project-id-3937556376642046437-f70aec5f1182.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';

/**
 Define contents here. The structure of the content is described in the next step.
*/
$content = [
  "url"=> "http://cuacuonchaua.com/",
  "type"=> "URL_UPDATED"
];

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

//update url
["type"=> "URL_UPDATED"]

//delete url
["type": "URL_DELETED"]

/*
send batch update: To reduce the number of HTTP connections your client has to make, you can combine up to 100 calls to the Indexing API into a single HTTP request
*/
$client->setUseBatch(true);
$batch = new Google_Http_Batch($client);
//http://docs.guzzlephp.org/en/stable/ | https://developers.google.com/api-client-library/php/guide/batch
/*$request = new \GuzzleHttp\Psr7\Request('POST', $endpoint, [ 'body' => json_encode($content) ]);
$batch->add($request, 'key1');*/

//init google batch and set root URL
$batch = new Google_Http_Batch($client,false,'https://indexing.googleapis.com');

//init service Notification to sent request
$postBody = new Google_Service_Indexing_UrlNotification();
$postBody->setType('URL_UPDATED');
$postBody->setUrl('https://your_job_detail');

//init service Indexing ( like updateJobPosting )
$service = new Google_Service_Indexing($client);
//create request
//$service->urlNotifications->createRequestUri('https://indexing.googleapis.com/batch');
$request_kame = $service->urlNotifications->publish($postBody);
//add request to batch
$batch ->add($request_kame);

$result = $batch->execute();
foreach($result['response-key1'] as $item) {
  echo $item['volumeInfo']['title'], "<br /> \n";	//for example
}


/*
	Get notification status
{
  url: “http://foo.com“
  latest_update {
    type: “URL_UPDATED”
    notify_time: “2017-07-31T19:30:54.524457662Z”
  }
  latest_remove {
    type: “URL_DELETED”
    notify_time: “2017-08-31T19:30:54.524457662Z”
  }
}    
*/
$httpClient->post('https://indexing.googleapis.com/v3/urlNotifications/metadata');

以上是关于php [gsearch console] google搜索控制台api #google的主要内容,如果未能解决你的问题,请参考以下文章

Facebook 分享者拒绝 goo.gl 链接

goo.gl公司使用API和API密钥缩短URL-v2

JS 1

sh Bash:goo.gl#使用Google URL Shortener服务(http://goo.gl)缩短网址。

如何在windows下拥有php console - 技术问答

php 与php-console的monolog