-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
祝文博
committed
Aug 30, 2019
0 parents
commit bc6a418
Showing
6 changed files
with
930 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` | ||
|
||
 | ||
|
||
``` | ||
jpy 1000 | ||
``` | ||
|
||
 | ||
|
||
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}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.