dounue1965 2018-08-15 21:08
浏览 142

如何将数据从Javascript传输到PHP?

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();
}

Then in PHP I validate this with:

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

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                              

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?

  • 写回答

1条回答 默认 最新

  • dongzhao3040 2018-08-16 14:08
    关注

    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();
    }
    

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体