Skip to content Skip to sidebar Skip to footer

How To Transport Data From Javascript To Php?

In Javascript I have: function authenticateAndFetch(payload) { const endpoint = PropertiesService.getScriptProperties().getProperty('WEBHOOK_URL'); const hmac_key = PropertiesS

Solution 1:

I never fixed the actual problem. And think minimizing the question will lead to a bug report on Google App Script or PHP. But here is a workaround that let's me keep moving, and it is highly inefficient:

functionauthenticateAndFetch(payload) {
  const endpoint = PropertiesService.getScriptProperties().getProperty('WEBHOOK_URL');
  const hmac_key = PropertiesService.getScriptProperties().getProperty('HMAC_SHA_256_KEY');
  var send_data = JSON.stringify(payload);
  send_data = Utilities.base64Encode(send_data); // https://stackoverflow.com/q/51866469/300224const signature = Utilities.computeDigest(Utilities.DigestAlgorithm.MD5, send_data)
    .map(function(chr){return (chr+256).toString(16).slice(-2)})
    .join(''); // Golf https://stackoverflow.com/a/49759368/300224const fetch_options = {
    'method': 'post',
    'payload': {'gmail_details': send_data, 'signature': signature},
    'muteHttpExceptions': true
  };
  const response = UrlFetchApp.fetch(endpoint, fetch_options);
  return response.getContentText();
}

And PHP

$detailsString = $_POST['gmail_details'];
$detailsString = base64_decode($_POST['gmail_details']);
$correctSignature = md5($detailsString);

In summary, you can't reliably transfer non-ASCII from Javascript to PHP so just base64 encode everything.

Post a Comment for "How To Transport Data From Javascript To Php?"