'How we can insert a horizontal rule/line in google docs using google docs API using PHP?

I want to insert a horizontal rule in google docs using PHP. I have also followed and gone through this link https://developers.google.com/apps-script/reference/document/paragraph#inserthorizontalrulechildindex but I am unable to create a horizontal line. I am trying to send the request with this next approach:

 $requests = [
    new Google_Service_Docs_Request([
         'insertHorizontalRule' => [
            'location' => [
               'index' => 1, 
            ],
        ]
    ]),
 ];

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

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

Can anybody tell me what am I missing or what mistake I am making.

getting this error -

PHP Fatal error:  Uncaught Google\Service\Exception: {
  "error": {
    "code": 400,
    "message": "Invalid JSON payload received. Unknown name \"insertHorizontalRule\" at 'requests[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"fields\" at 'requests[0]': Cannot find field.",
    "errors": [
      {
        "message": "Invalid JSON payload received. Unknown name \"insertHorizontalRule\" at 'requests[0]': Cannot find field.\nInvalid JSON payload received. Unknown name \"fields\" at 'requests[0]': Cannot find field.",
        "reason": "invalid"
      }
    ],
    "status": "INVALID_ARGUMENT",



Sources

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

Source: Stack Overflow

Solution Source