Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> API Keys and Access Tokens - PHP, problem z połączeniem
BartoszM007
post 10.01.2023, 10:31:00
Post #1





Grupa: Zarejestrowani
Postów: 1
Pomógł: 0
Dołączył: 10.01.2023

Ostrzeżenie: (0%)
-----


Dzień dobry,
Mam problem z połączeniem się z zewnętrznym API za pomocą skryptu PHP. Niestety zestawienie połączenia za pomocą tego skryptu nie działa. Problem jest podstawienie zmiennej typu string w miejsce klucza token i daty by za pomocą ich pobrać dane.

Poniżej kod jaki napisałem brakuje mi tylko funkcji lub poprawnej deklaracji zmiennych by wstawić klucz i czas, są one zadeklarowane jako $curl1 i $curl2.

<?php


$secret = '52';
$key = 'jcr';

function createHmacAuth($method, $path, $timestamp)
{
global $key, $secret;
$str = $method . ' ' . $path . $timestamp;
$hash = hash_hmac('sha256', $str, $secret);
return $key . ':' . $hash;
}

function getRequest($path)
{
$t = microtime(true);
$micro = sprintf("%03d", ($t - floor($t)) * 1000);
$time = gmdate('Y-m-d\TH:i:s.', $t) . $micro . 'Z';
$auth = createHmacAuth('POST', $path, $time);
echo $auth . "</pre>";
echo $time . "</pre>";
return $auth;
return $time;

}

$response = getRequest('');


$curl = curl_init();
$curl1 = curl_init($auth);
$curl2 = curl_init($time);
curl_setopt_array($curl, array(

CURLOPT_URL => 'https://...',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => '{
"machineSns": [
"IL3"
],
"dateFrom": "2022-03-30T08:47:32.591",
"dateTo": "2022-09-24T05:47:32.591",
"startRow": 0,
"endRow": 10,
"selectedKpiTab": "",
"searchString": "DG15460"
}',

CURLOPT_HTTPHEADER => array(


'Content-Type: application/json',
'x-hp-hmac-authentication:' . $curl1 ,
'x-hp-hmac-date:' . $curl2 ,
'x-hp-hmac-algorithm: SHA256 '
),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;


Dziękuję za pomoc.
facepalmxd.gif
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
3 Użytkowników czyta ten temat (3 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 28.03.2024 - 09:28