2017-10-10 150 views
1

我在PHP中使用Curl將API集成到我的網站中。我將檢索訪問令牌並使用它來處理供應商的訂單。RESTful API - 調用並使用令牌處理訂單

獲取令牌:

function getToken() { 
// authenticate and return $token 
$ch = curl_init(); 

$api_token = $this->config['API']; 
$ninjavan_id = $this->config['ID']; 
$ninjavan_secret = $this->config['SECRET']; 

curl_setopt($ch, CURLOPT_URL, $api_token); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
curl_setopt($ch, CURLOPT_HEADER, FALSE); 

curl_setopt($ch, CURLOPT_POST, TRUE); 

curl_setopt($ch, CURLOPT_POSTFIELDS, "{ 
    \"client_id\": \"$id\", 
    \"client_secret\": \"$secret\", 
    \"grant_type\": \"client_credentials\" 
}"); 

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "Content-Type: application/json", 
    "Accept: application/json" 
)); 

$response = curl_exec($ch); 
curl_close($ch); 

$json = json_decode($response, true); 
$file = 'key.txt'; 
$token = $json['access_token']; 

file_put_contents('./modules/custommodule/key.txt', $token, LOCK_EX); 
} 

我能夠成功地檢索令牌,我已經測試代碼。接下來將使用該令牌。

處理訂單:

$file = './modules/custommodule/key.txt'; 
$retrieved_token = file_get_contents($file); 

do { 
$retry = false; 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $api_order); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
curl_setopt($ch, CURLOPT_HEADER, FALSE); 
curl_setopt($ch, CURLOPT_POST, TRUE); 

curl_setopt($ch, CURLOPT_POSTFIELDS, "{ 
//curl execution 
}"); 

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "Content-Type: application/json", 
    "Accept: application/json", 
    "Authorization: Bearer $retrieved_token" 
)); 

$response = curl_exec($ch); 
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 

if ($httpcode === 401) { // unauthorized 
    getToken(); 
    $retry = true; 
    } 
} 
while ($retry); 
curl_close($ch); 

file_put_contents('./modules/custommodule/response.txt', $response, LOCK_EX); 
file_put_contents('./modules/custommodule/results.txt', $httpcode, LOCK_EX); 

任何援助不勝感激。如果你能指出刷新令牌的指導,那將是完美的。

謝謝。

+1

而不是直接輸入你的json POST字段,你應該使用帶'json_encode()'函數的數組清除你的代碼! – Odyssey1111

回答

0

簡單,使用字符串變量插值:

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "Content-Type: application/json", 
    "Accept: application/json", 
    "Authorization: Bearer {$token}" 
)); 

至於處理刷新令牌,你應該每次調用,如果有,因爲一個過期的令牌是一個錯誤遠程API之後檢查,而在這種情況下,得到一個新的令牌並重試該呼叫。如果你爲遠程API創建了某種代理,這個效果最好,所以這個機制對調用代碼來說是透明的。以下是你可能會怎麼做這是一個非常原始的例子:

var $token; 

function getToken() { 
    // authenticate and return $token as you did before 
} 

function callAnApiMethod($param1, $param2) { 
    global $token; 
    do { 
     $retry = false; 
     $ch = curl_init(); 
     // ... 
     // more setup things, using params of the function, etc. 
     // ... 
     curl_setopt($ch, CURLOPT_HTTPHEADER, array(
      "Content-Type: application/json", 
      "Accept: application/json", 
      "Authorization: Bearer {$token}" 
     )); 

     $res = curl_exec($ch); 
     $info = curl_getinfo($ch); 
     if ($info['http_code'] === 401) { // unauthorized 
      $token = getToken(); 
      $retry = true; 
     } 
    } while ($retry); 
    return json_decode($res); 
} 

老實說,我絕不會再經歷使用curl,如果我試圖消耗的API的巨大麻煩,嫋嫋是出奇的笨拙和冗長。我會用Guzzle之類的東西。


此外,作爲@ Odyssey1111指出的那樣,你可以使用數組符號來代表你的JSON,然後使用json_encode得到一個字符串:

$payload = [ 
    'field' => 'value', 
    'arrayField' => [ 
     'subfield' => 'subfieldValue', 
    ] 
]; 
$jsonStr = json_encode($payload); 

這使你的代碼更容易閱讀和維護。

+0

訪問令牌和訂單指向不同的URL。我正在尋找一種方法將這兩個過程合併成一個php文件。 – Enthu

+0

你可以,你只需要使用函數......基本上你可以將你的*取回代碼*代碼放在'getToken'函數中,但只要確保你不用'echo $ token'就可以返回$ token'。 –

+0

感謝您的提示,我將按照提議的功能加以解決。讓我今晚試試並更新結果。謝謝! – Enthu