'How can we insert text into Header/Footer after creating Header/Footer in google docs api using PHP?

I am working on google docs api. I want to insert content in header/footer in google docs using google docs API with PHP.

So, Firstly I am creating header/Footer request by the following request-

$requests[] = new Google_Service_Docs_Request(array(
  "createFooter" => [
    "sectionBreakLocation" => [
      "index" => 0,
      "segmentId" => "",
    ],
    "type" => "DEFAULT",
  ],
));

$requests[] = new Google_Service_Docs_Request(array(
  "createHeader" => [
    "sectionBreakLocation" => [
      "index" => 0,
      "segmentId" => "",
    ],
    "type" => "DEFAULT",
  ],
));

$batchUpdateRequest = new Google_Service_Docs_BatchUpdateDocumentRequest(array(
    'requests' => $requests
));

$response = $service->documents->batchUpdate($documentId, $batchUpdateRequest);

After this I am able to get the Header Id and Footer Id from the response, like this-

$footerId = $response->replies[0]->createFooter->footerId;
$headerId = $response->replies[1]->createHeader->headerId;
print("footerid".$footerId);
print("headerid".$headerId);

But, now I want to insert some texts in this above created header/footer.For that I was doing like this-

$requests[] = new Google_Service_Docs_Request(array(
  "createFooter" => [
    "sectionBreakLocation" => [
      "index" => 0,
      "segmentId" => "",
    ],
    "type" => "DEFAULT",
  ],
));

$requests[] = new Google_Service_Docs_Request(array(
  "createHeader" => [
    "sectionBreakLocation" => [
      "index" => 0,
      "segmentId" => "",
    ],
    "type" => "DEFAULT",
  ],
));

$batchUpdateRequest = new Google_Service_Docs_BatchUpdateDocumentRequest(array(
    'requests' => $requests
));

$response = $service->documents->batchUpdate($documentId, $batchUpdateRequest);

$footerId = $response->replies[0]->createFooter->footerId;
$headerId = $response->replies[1]->createHeader->headerId;
print("footerid".$footerId);
print("headerid".$headerId);


$requests [] = new Google_Service_Docs_Request([
  'insertText' => [
        'text' => "Sample7", 
        'location' => [
          'segmentId' => $footerId, 
          'index' => 0,
        ],
        'text' => 'sample text',
    ]
]);
$batchUpdateRequest = new Google_Service_Docs_BatchUpdateDocumentRequest(array(
  'requests' => $requests
));

$responses = $service->documents->batchUpdate($documentId, $batchUpdateRequest);

I thought by updating my batch again I can able to insert text in my header/footer, but unfortunately it is not the right way and I am stuck at this point.If this is not the way then how we can insert text into header/footer in google docs using PHP.



Solution 1:[1]

SUGGESTION

Perhaps you can try to structure your $requests code similar to an answer from How to insert text with google docs api php

Sample Tweaked Code:

$requests = array(new Google_Service_Docs_Request(array(
    'insertText' => array(
    'text' => 'sample text',
    'location' => array(
    'segmentId' => $footerId)))));

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1