$access_key = '';
$secret_key = '';
$base_url = 'https://api.corporatetools.com';
$request_path = '/companies';
$query_string = '?limit=1';
$request_body = '';
$header = json_encode([
'typ' => 'JWT',
'alg' => 'HS256',
'access_key' => $access_key,
]);
$payload = json_encode([
'path' => $request_path,
'content' => hash('sha256', $query_string . $request_body)
]);
$e_header = strreplace(['+', '/', '='], ['-', '', ''], base64_encode($header));
$e_payload = strreplace(['+', '/', '='], ['-', '', ''], base64_encode($payload));
$signature = hash_hmac('sha256', $e_header . '.' . $e_payload, $secret_key, true);
$e_signature = strreplace(['+', '/', '='], ['-', '', ''], base64_encode($signature));
$jwt = $e_header . '.' . $e_payload . '.' . $e_signature;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $base_url . $request_path . $query_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($ch, CURLOPT_POSTFIELDS, $request_body);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_HTTPGET, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization: Bearer $jwt",
'Content-Type: application/json',
'Accept: application/json'
]);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); // this results 0 every time
$http_res_code = curl_getinfo($ch, CURLINFO_RESPONSE_CODE); // this results 0 every time
if (curl_errno($ch)) {
die ('Error:' . curl_error($ch));
}
if (! empty($http_res_code) && $http_res_code <> 200) die ("http response code: ${http_res_code}");
curl_close($ch);
echo '<pre>';
echo json_encode(json_decode($result), JSON_PRETTY_PRINT);
echo '</pre>';
Hey Brian, how are you doing? I was looking into your code this morning and it looks like the ?
should be moved to a different line:
...
$query_string = 'limit=1';
...
curl_setopt($ch, CURLOPT_URL, $base_url . $request_path . '?' . $query_string);
...
I attempted to add an offset to the query string: $query_string = 'limit=1&offset=1';
but that too causes a 401 error so I am now looking into that and will let you know if I find anything.
query_string
to your payload content. Feel free to test it out and let me know if it works.
$access_key = '4399857f5067a649854bf3c61a344f5cb4bcbf5441ea7b036e0e7f6a90190b60cec71747e63091d0';
$secret_key = '5843be58de4803d03387adc0407580e20c69f80e72742f841adcdaebc30b72b82acf006edc4d69db';
$base_url = 'https://api.corporatetools.com';
//$query_string = '?company_id=ee6a6fb1-7007-4511-a4e4-96749ae3bca6&limit=1';
//$request_path = '/companies' . $query_string;
$query_string = '?url=http://www.labyrinthinc.com';
$request_path = '/registered-agent-products' . $query_string;
$request_body = '';
$header = json_encode([
'typ' => 'JWT',
'alg' => 'HS256',
'access_key' => $access_key,
]);
$payload = json_encode([
'path' => $request_path,
'content' => hash('sha256', $request_body)
]);
$e_header = str_replace(['+', '/', '='], ['-', '_', ''], base64_encode($header));
$e_payload = str_replace(['+', '/', '='], ['-', '_', ''], base64_encode($payload));
$signature = hash_hmac('sha256', $e_header . '.' . $e_payload, $secret_key, true);
$e_signature = str_replace(['+', '/', '='], ['-', '_', ''], base64_encode($signature));
$jwt = $e_header . '.' . $e_payload . '.' . $e_signature;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $base_url . $request_path);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_HTTPGET, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization: Bearer $jwt",
'Content-Type: application/json',
'Accept: application/json'
]);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); // this results 0 every time
$http_res_code = curl_getinfo($ch, CURLINFO_RESPONSE_CODE); // this results 0 every time
if (curl_errno($ch)) {
die ('Error:' . curl_error($ch));
}
if (! empty($http_res_code) && $http_res_code <> 200) echo ("http response code: ${http_res_code}");
curl_close($ch);
echo '<pre>';
echo json_encode(json_decode($result), JSON_PRETTY_PRINT);
echo '</pre>';
invalid website url
. What should be the website url?