Send data from gravity forms to GoogleSheets

时间:2016-10-20 19:06:27

标签: php wordpress forms gravity-forms-plugin

I'm using this code to submit data to 3rd party (GoogleSheets in this case) after form submission, the form I'm using has is '4', when I submit the form, the user is added but the data don't go to the Sheet for some reason:

add_action('gform_after_submission_4', 'add_to_google_spreadsheet', 10, 2);

function add_to_google_spreadsheet($entry, $form) {



$post_url = 'https://script.google.com/macros/s/xxxxxxxxxxx/exec';

$body = array('Phone' => rgar($entry, '2'), 'Email' => rgar($entry, '4'),);

$request = new WP_Http();
$response = $request->request($post_url, array('method' => 'POST', 'sslverify' => false, 'body' => $body));
}

this is the code I'm using for GoogleSheet web app

function doPost(e) {

  if (!e) return;

  var sheetID = "xxxxxxxxxxxxxxx";  // Replace this with the Google Spreadsheet ID
  var sheetName = "test";       // Replace this with the sheet name inside the Spreadsheet

  var status = {};

  // Code based on Martin Hawksey (@mhawksey)'s snippet

  var lock = LockService.getScriptLock();
  lock.waitLock(30000);

  try {

    var sheet = SpreadsheetApp.openById(sheetID).getSheetByName(sheetName);
    var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];

    // Add the data and time when the Gravity Form was submitted
    var column, row = [],
      input = {
        "timestamp": new Date()
      };

    for (var keys in e.parameter) {
      input[normalize_(keys)] = e.parameter[keys];
    }

    for (i in headers) {
      column = normalize_(headers[i])
      row.push(input[column] || "");
    }

    if (row.length) {

      sheet.appendRow(row);
      status = {
        result: "success",
        message: "Row added at position " + sheet.getLastRow()
      };

    } else {
      status = {
        result: "error",
        message: "No data was entered"
      };
    }

  } catch (e) {

    status = {
      result: "error",
      message: e.toString()
    };

  } finally {

    lock.releaseLock();

  }

  return ContentService
    .createTextOutput(JSON.stringify(status))
    .setMimeType(ContentService.MimeType.JSON);

}

function normalize_(str) {
  return str.replace(/[^\w]/g, "").toLowerCase();
}

here is google error after user submit

object(WP_Http)#4(0){

}array(6){
    ["headers"]=>object(Requests_Utility_CaseInsensitiveDictionary)#1256(1){
        ["data": protected]=>array(3){
            ["content-type"]=>string(24)"text/html; charset=UTF-8"["content-length"]=>string(4)"1555"["date"]=>string(29)"Thu, 20 Oct 2016 19:42:33 GMT"
        }
    }["body"]=>string(1555)" 
400. That’s an error.

Your client has issued a malformed or illegal request. That’s all we know. "["response"]=>array(2){
        ["code"]=>int(400)["message"]=>string(11)"Bad Request"
    }["cookies"]=>array(1){
        [0]=>object(WP_Http_Cookie)#1251(5){
            ["name"]=>string(3)"NID"["value"]=>string(131)"89=xxxxxxxxx_"["expires"]=>int(1492803753)["path"]=>string(1)"/"["domain"]=>string(10)"google.com"
        }
    }["filename"]=>NULL["http_response"]=>object(WP_HTTP_Requests_Response)#1260(5){
        ["response": protected]=>object(Requests_Response)#1257(10){
            ["body"]=>

0 个答案:

没有答案