title: Post data to google spreedsheet using php
body: <p>I am trying to post data using curl php but the issue I am facing the data saved in Google sheet is always coming undefined. I have used below code which is attached with description and also attached the image url which is showing the value coming from is php script I am using php and curl to send data and trying to savethat data in google sheet. </p>
  
  <p>Image showing undefined value: <a href="https://ibb.co/0n5WKdz" rel="nofollow noreferrer">https://ibb.co/0n5WKdz</a></p>
  
  <p><strong>PHP SCRIPT</strong></p>
  
  <pre><code>function send_to_url($url, $data)
  {
  $ch = curl_init($url);
  //Set a POST method
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  //curl_setopt($ch, CURLOPT_TIMEOUT, 6);
  
  //Set data to JSON application/json
  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
  $jsonDataEncoded = json_encode($data);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
  
  //POST to URL
  $result = curl_exec($ch);
  
  //Get result from POST if any
  $result_post = urldecode(json_encode($result));
  
  curl_getinfo($ch);
  curl_close($ch);
  return $result;
  }
  
  $users = 
  array("Date"=&gt;"Timestamp","FirstName"=&gt;"John","LastName"=&gt;"Cena","Email"=&gt;"john78@example.com");
  send_to_url("GOOGLE_APP_SCRIPT_URL",$users);
  </code></pre>
  
  <p><strong>GOOGLE SCRIPT</strong></p>
  
  <pre><code>var SHEET_NAME = "DATA";
  var SCRIPT_PROP = PropertiesService.getScriptProperties(); // new property service
  // If you don't want to expose either GET or POST methods you can comment out the appropriate function
  function doGet(e){
    return handleResponse(e);
  }
  
  function doPost(e){
    return handleResponse(e);
  }
  
  function handleResponse(e) {
  
    var lock = LockService.getPublicLock();
    lock.waitLock(30000);  // wait 30 seconds before conceding defeat.
  
    try {
      // next set where we write the data - you could write to multiple/alternate destinations
      var doc = SpreadsheetApp.openById(SCRIPT_PROP.getProperty("key"));
      var sheet = doc.getSheetByName(SHEET_NAME);
  
      // we'll assume header is in row 1 but you can override with header_row in GET/POST data
      var headRow = e.parameter.header_row || 1;
      var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
      var nextRow = sheet.getLastRow()+1; // get next row
      var row = []; 
      // loop through the header columns
      for (i in headers){
        if (headers[i] == "Timestamp"){ // special case if you include a 'Timestamp' column
          row.push(new Date());
        } else { // else use header name to get data
          row.push(e.parameter[headers[i]]);
        }
      }
      // more efficient to set values as [][] array than individually
      sheet.getRange(nextRow, 1, 1, row.length).setValues([row]);
      // return json success results
      return ContentService
            .createTextOutput(JSON.stringify({"result":"success", "row": headRow}))
            .setMimeType(ContentService.MimeType.JSON);
    } catch(e){
      // if error return this
      return ContentService
            .createTextOutput(JSON.stringify({"result":"error", "error": e}))
            .setMimeType(ContentService.MimeType.JSON);
    } finally { //release lock
      lock.releaseLock();
    }
  }
  
  function setup() {
      var doc = SpreadsheetApp.getActiveSpreadsheet();
      SCRIPT_PROP.setProperty("key", doc.getId());
  }
  </code></pre>
  
  <p>Please check my code and provide the solution and let me know where I am making mistake.</p>