Skip to content
This repository has been archived by the owner on Mar 29, 2020. It is now read-only.

Commit

Permalink
Added description and some code changes
Browse files Browse the repository at this point in the history
  • Loading branch information
vasilegoian committed Aug 16, 2016
1 parent d4b8c28 commit 0a05adc
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 11 deletions.
50 changes: 49 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,50 @@
# doublebit/okcoin
Laravel wrapper for OKCoin API.
A simple Laravel 5 wrapper for OKCoin API.

**This package is not ready for use in production!**

## Installation

Install with [composer](https://getcomposer.org):

`composer require doublebit/okcoin`

Then add the provider to `config/app.php`

`DoubleBit\OKCoin\OkcoinServiceProvider::class,`

and optionally add the facade to aliases

`'OKCoin' => DoubleBit\OKCoin\Facade::class,`

Then run `php artisan vendor:publish --provider="Doublebit\Okcoin\OkcoinServiceProvider`

## Usage

`echo json_encode(\OKCoin::getTicker())`


The package has no built-in API calls, you can make a call to any endpoint from the [OKCoin API](https://www.okcoin.com/about/rest_api.do).

If the OKCoin API adds new endpoints you can make calls to those without updating the package.

The methods are constructed as follows: first all lowercase word is the method (get/post).
Then starting with an uppercase letter is the name of the endpoint from OKCoin API.
In the name of the endpoint remove all the underscores (_) and start each word after an underscore
with an uppercase. Example: `POST /api/v1/batch_trade` becomes `OKCoin::postBatchTrade()`

The methods support up to 4 arguments, as follows:
* If the first argument is not a string, it MUST BE an array of parameters for the query (according to the API docs)
* If the first argument is string, it's considered API_KEY and the second argument MUST BE SECRET_KEY and the array
of parameters are moved to the third argument
* Last argument is always the callback. The callback receives the following 3 arguments: `$endpoint, $params, $result`.
The $endpoint is the API endpoint, not the method name. The $params is an array of params (sent to the method).
The $result is the json received from the server or `false` in case of error.

## Issues

Report any issues on [github](https://github.com/doublebit/okcoin/issues)

## License

See [LICENSE](https://github.com/doublebit/okcoin/blob/master/LICENSE) file
34 changes: 25 additions & 9 deletions src/DoubleBit/OKCoin/Okcoin.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
class Okcoin
{

/**
* @param $params array The parameters to
* @param $secret_key string
* @return string
*/
public function sign($params, $secret_key)
{
ksort($params);
Expand All @@ -19,15 +24,26 @@ public function __call($name, $arguments)
unset($pieces[0]);
$endpoint = strtolower(implode('_', $pieces));

$api_key = $arguments[0];
$secret_key = $arguments[1];
$params = $original_params = isset($arguments[2]) ? $arguments[2] : [];
$callback = isset($arguments[3]) ? $arguments[3] : null;

$params['api_key'] = $api_key;
$signature = $this->sign($params, $secret_key);
$query = http_build_query($params);
$query .= '&sign=' . $signature;
if (!isset($arguments[0]) || !is_string($arguments[0])) {
$api_key = \Config::get('okcoin.api_key');
$secret_key = \Config::get('okcoin.secret_key');
$params = $original_params = isset($arguments[0]) ? $arguments[0] : [];
$callback = isset($arguments[1]) ? $arguments[1] : null;
} else {
$api_key = $arguments[0];
$secret_key = $arguments[1];
$params = $original_params = isset($arguments[2]) ? $arguments[2] : [];
$callback = isset($arguments[3]) ? $arguments[3] : null;
}

if ($api_key && $secret_key) {
$params['api_key'] = $api_key;
$signature = $this->sign($params, $secret_key);
$query = http_build_query($params);
$query .= '&sign=' . $signature;
} else {
$query = http_build_query($params);
}
$result = $this->callApi(strtoupper($method), $endpoint, $query);
if (is_callable($callback)) {
call_user_func_array($callback, [$endpoint, $original_params, $result]);
Expand Down
4 changes: 3 additions & 1 deletion src/DoubleBit/OKCoin/config/okcoin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

return [

'api_version' => 'v1'
'api_version' => env('OKCOIN_API_VERSION', 'v1'),
'api_key' => env('OKCOIN_API_KEY', ''),
'secret_key' => env('OKCOIN_SECRET_KEY', ''),

];

0 comments on commit 0a05adc

Please sign in to comment.