1 回答

TA貢獻1874條經驗 獲得超12個贊
在ASAP Connected API 的文檔中,訪問令牌位于響應的標頭中。我只需要添加curl_setopt($curl, CURLOPT_HEADER, true);
以在響應中包含這些標頭,因此我的 PHP 請求如下所示:
$request_url = 'https://stagingapi.asapconnected.com/api/login';
$headers = array(
? ? //Accept or Content type
? ? 'Accept: application/json',
? ? //Authorisation
? ? 'Authorization: ' . http_build_query(array(
? ? ? ? 'user' => ASAPCONNECTED_USERNAME,
? ? ? ? 'password' => ASAPCONNECTED_PASSWORD,
? ? ? ? 'organizationId' => ASAPCONNECTED_ORGANIZATION_ID,
? ? ? ? 'apiKey' => ASAPCONNECTED_API_KEY
)));
$curl = curl_init($request_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); //Return response as string instead of display
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //Ignore any SSL errors
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); //Specify our header
curl_setopt($curl, CURLOPT_HEADER, true);//Include headers in response
curl_setopt($curl, CURLOPT_HTTPGET, true); //Specify that this is a GET request
$response = curl_exec($curl); //Execute cURL and collect the response
$curl_info = curl_getinfo($curl); //Retrieve it's info
$curl_error = curl_error($curl); //Retrieve it's error
curl_close($curl); //Close the cURL
if ($curl_error || !$response) {
? ? if ($curl_error) {
? ? ? ? //An error occurred requesting authorisation from the API!
? ? }
? ? if (!$response) {
? ? ? ? //The response was empty when requesting authorisation from the API!
? ? }
} else {
? ? //The API authorisation request was a success! Do stuff...
}
請注意:他們的支持團隊似乎對他們自己的產品并不是非常了解。因此,在要求提供 API 憑據時,請務必非常具體。我第一次詢問時,他們給了我一個臨時訪問令牌,該令牌將在一年多后到期。我第二次詢問時,他們給了我錯誤的憑據。第三次是一個魅力。不幸的是,您必須向他們開具票證才能獲取這些憑據,因為它們在任何地方的管理儀表板中都不可用。
如果其他人正在使用 PHP 使用此 API 并有任何疑問,我很樂意提供幫助,因為我也在這個旅程中。當這個項目結束時,我什至可能會寫一兩個教程。
- 1 回答
- 0 關注
- 187 瀏覽
添加回答
舉報