如何将数据从Javascript传输到PHP? [英] How to transport data from Javascript to PHP?

查看:71
本文介绍了如何将数据从Javascript传输到PHP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java语言中,我有:

In Javascript I have:

function authenticateAndFetch(payload) {
  const endpoint = PropertiesService.getScriptProperties().getProperty('WEBHOOK_URL');
  const hmac_key = PropertiesService.getScriptProperties().getProperty('HMAC_SHA_256_KEY');
  var send_data = JSON.stringify(payload)
  const 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/300224
  const fetch_options = {
    'method': 'post',
    'payload': {'gmail_details': send_data, 'signature': signature},
    'muteHttpExceptions': true
  };
  const response = UrlFetchApp.fetch(endpoint, fetch_options);
  return response.getContentText();
}

然后在PHP中,我通过以下方式对此进行验证:

Then in PHP I validate this with:

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

经过一番记录和漫长的一天,我发现Javascript和PHP将为包含以下数据的字符串产生不同的md5总和:

After some logging, and a long day, I found out that Javascript and PHP will produce different md5 sums for a string including this data:

HEX: 65 20 c2 97 20 44                              

在将它们发送到PHP之前,是否有某种方法可以清除Javascript中的输入,或者在两种环境中都可以通过某种方式获取匹配的字符串?

Is there some way to sanitize the inputs in Javascript before sending them to PHP, or is there some way to get matching strings in both environments?

推荐答案

我从未解决过实际问题.并且认为将问题最小化将导致有关Google App Script或PHP的错误报告.但这是一种解决方法,让我继续前进,它的效率非常低:

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:

function authenticateAndFetch(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/300224
  const 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/300224


  const fetch_options = {
    'method': 'post',
    'payload': {'gmail_details': send_data, 'signature': signature},
    'muteHttpExceptions': true
  };
  const response = UrlFetchApp.fetch(endpoint, fetch_options);
  return response.getContentText();
}

和PHP

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

总而言之,您无法可靠地将非ASCII从Javascript传输到PHP,因此仅对base64进行编码.

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

这篇关于如何将数据从Javascript传输到PHP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆