Skip to content

Instantly share code, notes, and snippets.

@laplasz
Last active January 23, 2021 16:35
Show Gist options
  • Save laplasz/d2f86a4f53dd36eb9172e3bd8ae14fd3 to your computer and use it in GitHub Desktop.
Save laplasz/d2f86a4f53dd36eb9172e3bd8ae14fd3 to your computer and use it in GitHub Desktop.
RestAPI access using shell script or php

Using HTTP Basic authentication where username is the api key and password is the calculated request signature that changes with every request. The signature is a hex-encoded HMAC-SHA1 hash calculated from the canonical request using provided secret. Canonical request takes form of {http method} {complete request path} {unix timestamp}, e.g. GET /v1/some/url?attributes=123&some=aaa 1548240417 Date header contains RFC 1123 compliant date format (The script can be used for access the Webonic API)

#!/bin/sh
time=`date +%s`
method='GET'
path='/v1/user'
api='https://rest.websupport.sk'
apiKey='<your_api_key>'
secret='<your_secret>'
canonicalRequest="$method $path $time"
signature=`echo -n $canonicalRequest| openssl sha1 -hmac $secret | sed -e 's/^.* //'`
curl $api$path -u $apiKey:$signature -H "Date: $(date -u -d @$time +%a,\ %d\ %b\ %Y\ %H:%M:%S\ %Z)"
<?php
$time = time();
$method = 'PUT';
$path = '/v1/user/self/zone/f/record/148126';
$api = 'https://rest.websupport.sk';
$apiKey = '<your_api_key>';
$secret = '<your_secret>';
$canonicalRequest = sprintf('%s %s %s', $method, $path, $time);
$signature = hash_hmac('sha1', $canonicalRequest, $secret);
$data = array(
'name' => '*',
'content' => '127.0.0.1',
'ttl' => '600'
);
$payload = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_URL, sprintf('%s%s', $api, $path));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $apiKey.':'.$signature);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Date: ' . gmdate('Ymd\THis\Z', $time),'Content-Type:application/json'
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
?>
#!/bin/sh
time=`date +%s`
method='PUT'
path='/v1/user/self/zone/f/record/148126'
api='https://rest.websupport.sk'
apiKey='<your_api_key>'
secret='<your_secret>'
canonicalRequest="$method $path $time"
signature=`echo -n $canonicalRequest| openssl sha1 -hmac $secret | sed -e 's/^.* //'`
curl $api$path -u $apiKey:$signature -H "Date: $(date -u -d @$time +%a,\ %d\ %b\ %Y\ %H:%M:%S\ %Z)" \
-H "Content-Type: application/json" -X $method -d '{"name":"*","content": "127.0.0.1","ttl": 600}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment