2015-10-14 1 views
1

J'essaie d'utiliser l'API GoodRx en utilisant PHP.Comment signer une charge utile avec une clé privée en PHP?

Voici mon 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 

Il est de renvoyer une erreur en disant que mon sig ne va pas.

Pourriez-vous m'aider s'il vous plait.

Merci.

Thomas.

Répondre

2

Vous ne faites pas correctement vos remplacements de chaîne:

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

Si vous voulez remplacer la chaîne, vous devez faire quelque chose comme:

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

Notez que vous passez dans le résultat du remplacement PRECEDENT dans le prochain appel, ce que vous ne faites pas. Vous continuez juste à prendre les originaux tring, remplacer une chose, puis jeter ce remplacement sur l'appel suivant. Donc, effectivement, vous faites seulement un remplacement / ->_, et en envoyant le + tel quel.

+0

J'ai fait la correction comme vous l'avez suggéré, mais j'ai quand même eu une erreur. –

2

ce détachement au cas où quelqu'un a besoin d'un exemple complet de la façon de faire un appel de base à l'API GoodRx:

En Python:

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) 

Vous devriez obtenir quelque chose comme ça pour la sortie:

{ "erreurs": [], "data": { "mobile_url": "http://m.goodrx.com/?grx_ref=api#/drug/atorvastatin/tablet", "forme": "tablette", "url": "http://www.goodrx.com/atorvastatin?grx_ref=api", "marque": ["lipitor"], "dosage": "20 mg", "prix": 12,0, "génériques": ["atorvastatine"], "quantité": 30, "affichage": "Lipitor (atorvastatine) », "fabricant": "générique"}, "succès": true}

Voici un code similaire en PHP, en regardant le médicament Apidra SoloStar qui a NDC 00088250205:

<?php 
function base64url_encode($data) { 
    return strtr(base64_encode($data), '+/', '__'); 
} 

$my_api_key = "YOUR_API_KEY"; 
$s_key="YOUR_SECRET_KEY"; 
$ndc = "00088250205"; 

// Initialize the CURL package. This is the thing that sends HTTP requests 
$ch = curl_init(); 

// Create the URL and the hash 
$url = "https://api.goodrx.com/fair-price?"; 

$query_string="ndc=" . $ndc . "&api_key=" . $my_api_key; 

$tmp_sig = hash_hmac('sha256', $query_string, $s_key, true); 
$sig = base64url_encode($tmp_sig); 

$url = $url . $query_string . "&sig=" . $sig; 

// set some curl options 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
curl_setopt($ch, CURLOPT_HEADER, FALSE); 
curl_setopt($ch, CURLOPT_VERBOSE, true); 

// run the query 
$response = curl_exec($ch); 

var_dump($response); 
?> 

Merci à Thomas d'avoir parlé avec moi à ce sujet.

+0

* Merci beaucoup d'avoir pris le temps de le faire, Len! Rien ne vaut "un exemple complet". –