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 关注
- 227 浏览
添加回答
举报
