Search code examples
phpgoogle-index

How to send batch request with Google Indexing API


I'm following Google indexing API

And I want to send batch request with Google_Service_Indexing.

In PHP, please help me. How to do it. In Google document, this is Google_Service_Books. I tried with PHP like :

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

And I stuck in error :

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: /


Solution

  • I tried with PHP and completed.

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