Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
祝文博 committed Aug 30, 2019
0 parents commit bc6a418
Show file tree
Hide file tree
Showing 6 changed files with 930 additions and 0 deletions.
56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Currency

Alfred Workflow Currency Exchange. Convert any currency to CNY.

## API

The free API is from juhe.cn

Please apply from here:
[https://www.juhe.cn/docs/api/id/80](https://www.juhe.cn/docs/api/id/80)

Then fill in the appkey in ```CurrencyExchange.php```.

```php
<?php

class CurrencyExchange
{
// Please fill in appkey here.
private $_appKey = "appkey";
private $_api = "http://op.juhe.cn/onebox/exchange/currency";
...
```

## Usage
```
usd 100
```

![usd](usd.jpg)

```
jpy 1000
```

![jpy](jpy.jpg)

Press enter than the result will copy to you clipboard.

## Customize

Here is the code in workflow editor.

```php
require_once('CurrencyExchange.php');

$currencyExchange = new CurrencyExchange();

$currencyExchange->caculate("USD", "CNY", {query});
```

You can convert any currency to another by modify the code of last line, like JPY to USD.

```
$currencyExchange->caculate("JPY", "USD", {query});
```
79 changes: 79 additions & 0 deletions Source/CurrencyExchange.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

class CurrencyExchange
{
// Please fill in appkey here.
private $_appKey = "appkey";
private $_api = "http://op.juhe.cn/onebox/exchange/currency";

function caculate($from, $to, $amount)
{
header('Content-type:text/html;charset=utf-8');

$params = array(
"from" => $from,
"to" => $to,
"key" => $this->_appKey,
);
$paramstring = http_build_query($params);
$content = $this->juhecurl($this->_api, $paramstring);
$result = json_decode($content, true);
if ($result) {
if ($result['error_code'] == '0') {
$exchange = $result["result"][0]["exchange"];
$output = $exchange * $amount;

$json = [
"items" => [
[
"title" => $output,
"subtitle" => $from . " / " . $to . " = " . $output,
"arg" => $output
]
]
];
echo json_encode($json);

} else {
echo $result['error_code'] . ":" . $result['reason'];
}
} else {
echo "请求失败";
}
}

function juhecurl($url, $params = false, $ispost = 0)
{
$httpInfo = array();
$ch = curl_init();

curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_USERAGENT, 'JuheData');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
if ($ispost) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_URL, $url);
} else {
if ($params) {
curl_setopt($ch, CURLOPT_URL, $url . '?' . $params);
} else {
curl_setopt($ch, CURLOPT_URL, $url);
}
}
$response = curl_exec($ch);
if ($response === FALSE) {
return false;
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$httpInfo = array_merge($httpInfo, curl_getinfo($ch));
curl_close($ch);
return $response;
}
}

//$currencyExchange = new CurrencyExchange();
//$currencyExchange->caculate("USD", "CNY", 1000);
Binary file added Source/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit bc6a418

Please sign in to comment.