I'm working with this not-so-great ASAP Connected documentation. It's written for .NET, but I'm creating a WordPress plugin in PHP with it for a client.
To get authorisation, it's providing this snippet:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://stagingapi.asapconnected.com/api/login");
request.Headers.Add(HttpRequestHeader.Authorization, "user=username&organizationId=id&password=password&apiKey=apikey");
request.Accept = "application/json";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string accessToken = response.Headers["asap_accesstoken"];
In my plugin, I'm attempting to do this with this:
$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_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...
}
So I thought that the "GetResponse" is like a GET request, and the documentation and it's snippet very clearly put the API credentials in an "Authorization" header. However, the response I get is empty! It doesn't return any errors. When I log the $curl_info
variable, it outputs this (some data omitted):
[01-Jul-2020 21:03:36 UTC] Array
(
[url] => https://stagingapi.asapconnected.com/api/login
[content_type] =>
[http_code] => 401
[header_size] => 765
[request_size] => 222
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.183425
[namelookup_time] => 0.00508
[connect_time] => 0.006237
[pretransfer_time] => 0.047479
[size_upload] => 0
[size_download] => 0
[speed_download] => 0
[speed_upload] => 0
[download_content_length] => 0
[upload_content_length] => -1
[starttransfer_time] => 0.183389
[redirect_time] => 0
[redirect_url] =>
[certinfo] => Array
(
)
[http_version] => 2
[protocol] => 2
[ssl_verifyresult] => 0
[scheme] => HTTPS
[appconnect_time_us] => 47412
[connect_time_us] => 6237
[namelookup_time_us] => 5080
[pretransfer_time_us] => 47479
[redirect_time_us] => 0
[starttransfer_time_us] => 183389
[total_time_us] => 183425
)
Any ideas?