dourao1896 2015-10-14 15:06
浏览 30

如何使用PHP中的私钥签署有效负载?

I am trying to utilize the GoodRx API using PHP.

Here is my code:

 $hash = hash_hmac('sha256', $query_string, MY_SECRET_KEY);
 $encoded = base64_encode($hash);
 $private_key = str_replace('+', '_', $encoded);
 $private_key = str_replace('/', '_', $encoded);
 //$private_key = urlencode($private_key);
 $query_string .= '&sig=' . $private_key;

 echo $query_string;    
 // https://api.goodrx.com/low-price?name=Lipitor&api_key=MY_API_KEY&sig=MY_SECRET_KEY

It is returning an error saying that my sig is not right.

Could you help me please.

Thank you.

Thomas.

  • 写回答

2条回答 默认 最新

  • dorkahemp972157683 2015-10-14 15:10
    关注

    You're not doing your string replacements correctly:

    $private_key = str_replace('+', '_', $encoded);
       ^^---new string                       ^---original string
    $private_key = str_replace('/', '_', $encoded);
       ^--overwrite previous replacement     ^---with original string again
    

    If you want to chain replacements, you have to do something more like:

    $orig = 'foo';
    $temp = str_replace(..., $orig);
    $temp = str_replace(..., $temp);
    ...
    $final = str_replace(..., $temp);
    

    Note how you pass in the result of the PREVIOUS replacement into the next call, which is what you're not doing. You just keep taking the originals tring, replace one thing, then trash that replacement on the next call. So effectively you're ONLY doing a /->_ replacement, and sending the + as-is.

    评论

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题