有没有人设法使用php访问pritunl的api? 源代码https://github.com/pritunl/pritunl/blob/master/tools/test_pritunl.py提供的指南只有python示例。 如果我尝试读取数据,它工作正常。但是当我尝试更新或创建新实体时,我收到HTTP请求失败! HTTP / 1.1 401 UNAUTHORIZED
我的代码是:
public static function auth_request($method, $path, $data="")
$BASE_URL = env('PRITUNL_BASE_URL');
$API_TOKEN = env('PRITUNL_API_TOKEN');
$API_SECRET = env('PRITUNL_API_SECRET');
$auth_timestamp = strval(time());
$auth_nonce = uniqid();
$auth_array = array(
$API_TOKEN,
$auth_timestamp,
$auth_nonce,
strtoupper($method),
$path,
);
if ($data) {
array_push($auth_array, $data);
}
$auth_string = join("&", $auth_array);
$auth_signature = base64_encode(hash_hmac(
"sha256", $auth_string, $API_SECRET, true));
$options = array(
"http" => array(
"header" => array(
'Content-Type: application/json',
'Auth-Token: '.$API_TOKEN,
'Auth-Timestamp: '.$auth_timestamp,
'Auth-Nonce: '.$auth_nonce,
'Auth-Signature: '.$auth_signature
),
"method" => $method,
"content" => $data,
),
"ssl" => array(
"allow_self_signed" => true,
"verify_peer_name" => false,
),
);
$context = stream_context_create($options);
return file_get_contents($BASE_URL.$path, false, $context);
答案 0 :(得分:0)
解决了从$ auth_array中删除$数据的问题。 这是我的新代码:
public static function auth_request($method, $path, $data="") {
$BASE_URL = env('PRITUNL_BASE_URL');
$API_TOKEN = env('PRITUNL_API_TOKEN');
$API_SECRET = env('PRITUNL_API_SECRET');
$auth_timestamp = strval(time());
$auth_nonce = uniqid();
$auth_array = array(
$API_TOKEN,
$auth_timestamp,
$auth_nonce,
strtoupper($method),
$path,
);
$auth_string = join("&", $auth_array);
$auth_signature = base64_encode(hash_hmac(
"sha256", $auth_string, $API_SECRET, true));
$options = array(
"http" => array(
"header" => array(
'Content-Type: application/json',
'Auth-Token: '.$API_TOKEN,
'Auth-Timestamp: '.$auth_timestamp,
'Auth-Nonce: '.$auth_nonce,
'Auth-Signature: '.$auth_signature
),
"method" => $method,
"content" => $data,
),
"ssl" => array(
"allow_self_signed" => true,
"verify_peer_name" => false,
),
);
$context = stream_context_create($options);
return file_get_contents($BASE_URL.$path, false, $context);
}