如何使用 Google 索引 API 发送批量请求

How to send batch request with Google Indexing API

我正在关注 Google 索引 API

我想用 Google_Service_Indexing 发送批量请求。

在PHP,请帮帮我。怎么做。 在 Google 文档中,这是 Google_Service_Books。 我试过 PHP 像 :

      //set google client
      $client = new Google_Client();
      $client->setAuthConfig('my_key.json');
      $client->addScope('https://www.googleapis.com/auth/indexing');
      $client->setUseBatch(true);
      //set batch
      $batch = new Google_Http_Batch($client);
      //set service
      $service = new Google_Service_Indexing($client);

      $postBody = new Google_Service_Indexing_UrlNotification();
      $postBody->setType('URL_UPDATED');
      $postBody->setUrl('https://my_job_detail');
      $service->urlNotifications->publish($postBody);
      $batch ->add($service);
      $results = $batch->execute();

我陷入了错误:

Argument 1 passed to Google_Http_Batch::add() must implement interface Psr\Http\Message\RequestInterface, instance of Google_Service_Indexing_UrlNotification given, called in /Applications/MAMP/htdocs/Jukukoushi/src/Controller/ToppagesController.php on line 340 Request URL: /

我尝试 PHP 并完成了。

   //init google client
    $client = new Google_Client();
    $client->setAuthConfig('your_path_to_key.json');
    $client->addScope('https://www.googleapis.com/auth/indexing');
    $client->setUseBatch(true);

    //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);