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

时间:2015-10-14 15:06:27

标签: php rest signing

我正在尝试使用PHP来使用GoodRx API。

这是我的代码:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int v1=1, v2=0, v3=1;

    int result;

    result = (v1 << 2) | (v2 << 1) | v3;

    printf("%d\n", result);

    return 0;
}

它返回一个错误,说我的信号不对。

你能帮帮我吗?

谢谢。

托马斯。

2 个答案:

答案 0 :(得分:2)

您没有正确执行字符串替换:

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

如果你想链接替换,你必须做更多的事情:

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

请注意您如何将PREVIOUS替换的结果传入下一个电话,这是您不会做的。你只需要继续使用原件,更换一件东西,然后在下一次调用时删除该替换件。因此,您只需要进行/ - &gt; _替换,然后按原样发送+

答案 1 :(得分:2)

发布此信息,以防任何人需要有关如何对GoodRx API进行基本调用的完整示例:

在Python中:

ObservableCollection<AC_SCSDraw> scsDraw = new ObservableCollection<AC_SCSDraw>();
    public ObservableCollection<AC_SCSDraw> SCSDraw
    {
        get { return scsDraw; }
        set
        {
            scsDraw = value;
            OnPropertyChanged("SCSDraw");
        }
    }
public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        var changed = PropertyChanged;
        if (changed != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

你应该为输出得到这样的东西:

  

{&#34;错误&#34;:[],&#34;数据&#34;:{&#34; mobile_url&#34;:   &#34; http://m.goodrx.com/?grx_ref=api#/drug/atorvastatin/tablet&#34;,&#34;表格&#34;:   &#34;平板电脑&#34;,&#34;网址&#34;:&#34; http://www.goodrx.com/atorvastatin?grx_ref=api&#34;,   &#34;品牌&#34;:[&#34; lipitor&#34;],&#34;剂量&#34;:&#34; 20mg&#34;,&#34;价格&#34;:12.0,& #34;通用&#34 ;:   [&#34;阿托伐他汀&#34;],&#34;数量&#34;:30,&#34;显示&#34;:&#34;立普妥(阿托伐他汀)&#34;,   &#34;制造商&#34;:&#34;通用&#34;},&#34;成功&#34;:true}

这里有类似的PHP代码,查找药物Apidra Solostar,它有NDC 00088250205:

import requests
import hashlib
import hmac
import base64

# python --version returns:
#        Python 3.5.1 :: Anaconda 2.4.1 (32-bit)

# my_api_key is the API key for your account, provided by GoodRx
my_api_key = "YOUR_API_KEY";

# s_skey is the secret key for your account, provided by GoodRx
my_secret_key=str.encode("YOUR_SECRET_KEY", 'utf-8')

# Create the base URL and the URL parameters
# The url_params start as text and then have to be encoded into bytes
url_base = "https://api.goodrx.com/fair-price?"
url_params = str.encode("name=lipitor&api_key=" + my_api_key, 'utf-8')

# This does the hash of url_params with my_secret_key    
tmpsig = hmac.new(my_secret_key, msg=url_params,     digestmod=hashlib.sha256).digest()

# Base64 encoding gets rid of characters that can't go in to URLs. 
# GoodRx specifically asks for these to be replaced with "_"
sig = base64.b64encode(tmpsig, str.encode("__", 'utf-8') )    

# Convert the sig back to ascii
z = sig.decode('ascii')

# add the sig to the URL base
url_base += url_params.decode('ascii') + "&sig=" + z

# request the URL base
r = requests.get(url_base)

# print the response
print(r.content)

感谢托马斯就此与我交谈。