Skip to content

Instantly share code, notes, and snippets.

@gregbird
Last active January 20, 2020 21:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gregbird/cd904ff230cdf86253716aa351154edb to your computer and use it in GitHub Desktop.
Save gregbird/cd904ff230cdf86253716aa351154edb to your computer and use it in GitHub Desktop.
Use this Scheduler to get the Tax rates every hour from the following API https://api.taxrates.io/index.html#method_getrates
<?php
function help() {
echo <<<TEXT
Taxrates scheduler v1.0
Usage:
php scheduler.php clientSecret command arg1 arg2 ...
php scheduler.php clientSecret rates [zip] [filter]
php scheduler.php clientSecret countrycode code product_code [zip] [filter]
php scheduler.php clientSecret ip ipaddr product_code [zip] [filter]
Examples:
php scheduler.php W0ARtUS52Dfwx3LnSy8 rates
php scheduler.php W0ARtUS52Dfwx3LnSy8 rates 10006
php scheduler.php W0ARtUS52Dfwx3LnSy8 rates 10006 standard
php scheduler.php W0ARtUS52Dfwx3LnSy8 countrycode DE C010
php scheduler.php W0ARtUS52Dfwx3LnSy8 countrycode US C010 10006
php scheduler.php W0ARtUS52Dfwx3LnSy8 countrycode US C010 10006 standard
php scheduler.php W0ARtUS52Dfwx3LnSy8 ip 8.8.8.8 C010
php scheduler.php W0ARtUS52Dfwx3LnSy8 ip 4.4.4.4 C010 10006
php scheduler.php W0ARtUS52Dfwx3LnSy8 ip 8.8.8.8 C010 10006 standard
TEXT;
}
if ($argc < ($argv[2] === 'rates' ? 3 : 4)) {
help();
return 1;
}
$curl = new class($argv[1])
{
private $ch;
public function __construct($clientSecret)
{
$this->ch = curl_init();
curl_setopt($this->ch, CURLOPT_HEADER, 0);
curl_setopt($this->ch, CURLOPT_FAILONERROR, false);
curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
// curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false);
// curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($this->ch, CURLOPT_HTTPHEADER, [
'Accept: application/json',
'Authorization: Apikey ' . $clientSecret,
]);
}
public function __destruct()
{
curl_close($this->ch);
}
public function rates($zip = null, $filter = null)
{
$query = http_build_query(
[
'domain' => 'api.taxrates.io',
'zip' => $zip,
'filter' => $filter,
]
);
return $this->request('/api/v1/tax/rates?'.$query);
}
public function rates3($cursor = 0, $filter = false)
{
$query = http_build_query([
'domain' => 'api.taxrates.io',
'cursor' => $cursor,
'filter' => $filter,
]);
return $this->request('/api/v3/tax/rates?'.$query);
}
public function countryCode($countryCode, $productCode, $zip = null, $filter = null)
{
$query = http_build_query(
[
'domain' => 'api.taxrates.io',
'country_code' => $countryCode,
'zip' => $zip,
'product_code' => $productCode,
'filter' => $filter,
]
);
return $this->request('/api/v1/tax/countrycode?'.$query);
}
public function ip($ip, $productCode, $zip = null, $filter = null)
{
$query = http_build_query(
[
'domain' => 'api.taxrates.io',
'ip' => $ip,
'zip' => $zip,
'product_code' => $productCode,
'filter' => $filter,
]
);
return $this->request('/api/v1/tax/ip?'.$query);
}
private function request($url)
{
$headers = [];
// $domain = 'http://api.stage.taxrates.io';
// $domain = 'http://api.taxrates.local';
$domain = 'https://api.taxrates.io';
curl_setopt($this->ch, CURLOPT_URL, $domain.$url);
curl_setopt($this->ch, CURLOPT_HEADERFUNCTION, function ($curl, $header) use(&$headers) {
$len = strlen($header);
$header = explode(':', $header, 2);
if (count($header) < 2) { // Invalid header
return $len;
}
$name = strtolower(trim($header[0]));
if (!array_key_exists($name, $headers)) {
$headers[$name] = [trim($header[1])];
} else {
$headers[$name][] = trim($header[1]);
}
return $len;
});
$result = curl_exec($this->ch);
if ($result === false) {
throw new RuntimeException(sprintf(
'Curl: (%d) %s',
curl_errno($this->ch),
curl_error($this->ch)
));
}
if (curl_getinfo($this->ch, CURLINFO_RESPONSE_CODE) >= 400) {
throw new RuntimeException($result);
}
$result = json_decode($result, true);
if (!empty($result['ErrorCode'])) {
throw new RuntimeException($result['ErrorMessage']);
}
return [
'result' => $result,
'headers' => $headers,
];
}
};
try {
$args = array_pad(array_slice($argv, 2, 5), 5, null);
switch ($args[0]) {
case 'rates':
$response = $curl->rates($args[1], $args[2]);
break;
case 'countrycode':
$response = $curl->countryCode($args[1], $args[2], $args[3], $args[4]);
break;
case 'ip':
$response = $curl->ip($args[1], $args[2], $args[3], $args[4]);
break;
case 'rates3':
$cursor = 0;
while ($cursor !== null) {
$response = $curl->rates3($cursor);
$output = substr(json_encode($response['result']), 0, -1);
if ($cursor !== 0) {
$output = ','.substr($output, 1);
}
fwrite(STDOUT, $output);
sleep(5);
$cursor = isset($response['headers']['x-cursor-next'][0]) ? $response['headers']['x-cursor-next'][0] : null;
}
fwrite(STDOUT, ']');
return;
default:
help();
return 1;
}
$output = json_encode($response['result'], JSON_PRETTY_PRINT);
if ($args[0] === 'rates3') {
fwrite(STDOUT, substr(substr($output, 0, -1).',', 1));
} else {
fwrite(STDOUT, $output);
}
} catch (Exception $e) {
fwrite(STDERR, $e->getMessage());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment