Skip to content

Commit 056fb04

Browse files
committed
first release
0 parents  commit 056fb04

11 files changed

+547
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
vendor/*
2+
index.php

README.md

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# MEXC-API PHP
2+
lib mexc api simples, para ver detalhes da conta, saldo spot, withdraw
3+
info token, etc...
4+
5+
# Instalação
6+
7+
```shell
8+
composer require wilianmaique/php-mexc-api-v3
9+
```
10+
11+
# Como usar
12+
13+
add to your config
14+
```shell
15+
const MEXC_CONFIG = [
16+
'MEXC_URL_API' => 'https://api.mexc.com/api/v3',
17+
'MEXC_API_ACCESS_KEY' => 'xxxxxx',
18+
'MEXC_API_SECRET' => 'xxxxxxxxxxxxxxxxx'
19+
];
20+
```
21+
22+
Account::get()
23+
```PHP
24+
<?php
25+
require __DIR__ . '/vendor/autoload.php';
26+
27+
use WilianMaique\Mexc\Mexc\Account;
28+
29+
// detail account
30+
var_dump(Account::get());
31+
32+
//output
33+
34+
/*
35+
array (size=11)
36+
'makerCommission' => int 0
37+
'takerCommission' => int 0
38+
'buyerCommission' => int 0
39+
'sellerCommission' => int 0
40+
'canTrade' => boolean true
41+
'canWithdraw' => boolean true
42+
'canDeposit' => boolean true
43+
'updateTime' => null
44+
'accountType' => string 'SPOT' (length=4)
45+
'balances' =>
46+
array (size=2)
47+
0 =>
48+
array (size=3)
49+
'asset' => string 'USDT' (length=4)
50+
'free' => string '0.000000006' (length=11)
51+
'locked' => string '0' (length=1)
52+
1 =>
53+
array (size=3)
54+
'asset' => string 'BNB' (length=3)
55+
'free' => string '0.000244271466267283' (length=20)
56+
'locked' => string '0' (length=1)
57+
'permissions' =>
58+
array (size=1)
59+
0 => string 'SPOT' (length=4)
60+
*/
61+
```
62+
63+
InfoToken::get('WEMIX')
64+
```PHP
65+
<?php
66+
require __DIR__ . '/vendor/autoload.php';
67+
68+
use WilianMaique\Mexc\Mexc\InfoToken;
69+
70+
// get info by token name or all tokens
71+
var_dump(InfoToken::get('WEMIX'));
72+
73+
//output
74+
75+
/*
76+
array (size=3)
77+
'coin' => string 'WEMIX' (length=5)
78+
'name' => string 'WEMIX TOKEN' (length=11)
79+
'networkList' =>
80+
array (size=1)
81+
0 =>
82+
array (size=15)
83+
'coin' => string 'WEMIX' (length=5)
84+
'depositDesc' => null
85+
'depositEnable' => boolean true
86+
'minConfirm' => int 60
87+
'name' => string 'WEMIX TOKEN' (length=11)
88+
'network' => string 'WEMIX' (length=5)
89+
'withdrawEnable' => boolean true
90+
'withdrawFee' => string '0.100000000000000000' (length=20)
91+
'withdrawIntegerMultiple' => null
92+
'withdrawMax' => string '550000.000000000000000000' (length=25)
93+
'withdrawMin' => string '1.000000000000000000' (length=20)
94+
'sameAddress' => boolean false
95+
'contract' => string '' (length=0)
96+
'withdrawTips' => null
97+
'depositTips' => null
98+
*/
99+
```
100+
101+
#
102+
# All functions....
103+
104+
```PHP
105+
Account::get(); // get detail account
106+
Account::getSpotBalance(); // get balance by asset, null to list all
107+
InfoToken::get(); // get info token, null to list all
108+
PriceTicker::get(); // get price by token, null to list all
109+
Withdraw::withdraw('WEMIX', 'WEMIX', 'xxxxxxxx', '1.0'); // send crypto to any wallet, if ok [id] returns
110+
Withdraw::history('WEMIX', 'c3bbe527692742cc95e64f999dfda324'); // get all history, if 'withdrawId' is not defined list all
111+
```

composer.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "wilianmaique/php-mexc-api-v3",
3+
"description": "easy to use mexc api wallet",
4+
"type": "library",
5+
"license": "GPL-3.0-or-later",
6+
"authors": [
7+
{
8+
"name": "Wilian Maique"
9+
}
10+
],
11+
"autoload": {
12+
"psr-4": {
13+
"WilianMaique\\Mexc\\": "src/"
14+
}
15+
},
16+
"require": {
17+
"php": ">=8.1"
18+
}
19+
}

composer.lock

+20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Mexc/Account.php

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace WilianMaique\Mexc\Mexc;
4+
5+
class Account extends Time
6+
{
7+
/*
8+
* get detail account
9+
*/
10+
public static function get(): array|bool
11+
{
12+
$buildQuery = [
13+
'recvWindow' => 10000,
14+
'timestamp' => Time::time(5000)
15+
];
16+
17+
$url = MEXC_CONFIG['MEXC_URL_API'] . '/account?' . BuildHttpQuery::build($buildQuery) . '&signature=' . Signature::signature($buildQuery);
18+
$ch = curl_init($url);
19+
20+
curl_setopt_array($ch, [
21+
CURLOPT_CUSTOMREQUEST => 'GET',
22+
CURLOPT_HTTPHEADER => [
23+
'X-MEXC-APIKEY: ' . MEXC_CONFIG['MEXC_API_ACCESS_KEY'] . ''
24+
],
25+
CURLOPT_RETURNTRANSFER => true,
26+
CURLOPT_SSL_VERIFYPEER => false,
27+
]);
28+
29+
$res = curl_exec($ch);
30+
31+
if (!$res) {
32+
curl_close($ch);
33+
return false;
34+
}
35+
36+
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
37+
curl_close($ch);
38+
39+
if ($status_code != 200)
40+
return json_decode($res, true);
41+
42+
return json_decode($res, true);
43+
}
44+
45+
/*
46+
* get spot balance by name ou null to all
47+
*/
48+
public static function getSpotBalance(string $assetName = null): array|bool
49+
{
50+
$assetName = strtoupper($assetName ?? '');
51+
52+
$r = self::get();
53+
54+
if (!$r)
55+
return false;
56+
57+
if (empty($assetName)) {
58+
return $r['balances'];
59+
}
60+
61+
$balances = array_filter($r['balances'], function ($balance) use ($assetName) {
62+
return $balance['asset'] === $assetName;
63+
});
64+
65+
$balances = reset($balances);
66+
67+
if (!$balances)
68+
return $r['balances'];
69+
70+
return $balances;
71+
}
72+
}

src/Mexc/BuildHttpQuery.php

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace WilianMaique\Mexc\Mexc;
4+
5+
class BuildHttpQuery
6+
{
7+
public static function build(array $params): string
8+
{
9+
ksort($params);
10+
return http_build_query($params);
11+
}
12+
}

src/Mexc/InfoToken.php

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
namespace WilianMaique\Mexc\Mexc;
4+
5+
class InfoToken extends Time
6+
{
7+
/*
8+
*
9+
* get info token by name or null to get all list
10+
*/
11+
public static function get(string $tokenCoinName = null): array|bool
12+
{
13+
$tokenCoinName = strtoupper($tokenCoinName ?? '');
14+
15+
$buildQuery = [
16+
'recvWindow' => 10000,
17+
'timestamp' => Time::time(5000)
18+
];
19+
20+
$url = MEXC_CONFIG['MEXC_URL_API'] . '/capital/config/getall?' . BuildHttpQuery::build($buildQuery) . '&signature=' . Signature::signature($buildQuery);
21+
$ch = curl_init($url);
22+
23+
curl_setopt_array($ch, [
24+
CURLOPT_CUSTOMREQUEST => 'GET',
25+
CURLOPT_HTTPHEADER => [
26+
'X-MEXC-APIKEY: ' . MEXC_CONFIG['MEXC_API_ACCESS_KEY'] . ''
27+
],
28+
CURLOPT_RETURNTRANSFER => true,
29+
CURLOPT_SSL_VERIFYPEER => false,
30+
]);
31+
32+
$res = curl_exec($ch);
33+
34+
if (!$res) {
35+
curl_close($ch);
36+
return false;
37+
}
38+
39+
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
40+
$resJson = json_decode($res, true);
41+
curl_close($ch);
42+
43+
if ($status_code != 200)
44+
return $resJson;
45+
46+
if (empty($tokenCoinName))
47+
return $resJson;
48+
49+
$info = [];
50+
51+
foreach ($resJson as $resJsonValue) {
52+
if ($resJsonValue['coin'] === $tokenCoinName) {
53+
$info['coin'] = $tokenCoinName;
54+
$info['name'] = $resJsonValue['name'];
55+
56+
foreach ($resJsonValue['networkList'] as $key => $value) {
57+
$info['networkList'][$key]['coin'] = $value['coin'];
58+
$info['networkList'][$key]['depositDesc'] = $value['depositDesc'];
59+
$info['networkList'][$key]['depositEnable'] = $value['depositEnable'];
60+
$info['networkList'][$key]['minConfirm'] = $value['minConfirm'];
61+
$info['networkList'][$key]['name'] = $value['name'];
62+
$info['networkList'][$key]['network'] = $value['network'];
63+
$info['networkList'][$key]['withdrawEnable'] = $value['withdrawEnable'];
64+
$info['networkList'][$key]['withdrawFee'] = $value['withdrawFee'];
65+
$info['networkList'][$key]['withdrawIntegerMultiple'] = $value['withdrawIntegerMultiple'];
66+
$info['networkList'][$key]['withdrawMax'] = $value['withdrawMax'];
67+
$info['networkList'][$key]['withdrawMin'] = $value['withdrawMin'];
68+
$info['networkList'][$key]['sameAddress'] = $value['sameAddress'];
69+
$info['networkList'][$key]['contract'] = $value['contract'];
70+
$info['networkList'][$key]['withdrawTips'] = $value['withdrawTips'];
71+
$info['networkList'][$key]['depositTips'] = $value['depositTips'];
72+
}
73+
74+
if (count($resJsonValue['networkList']) <= 0)
75+
reset($info['networkList']);
76+
77+
break;
78+
}
79+
}
80+
81+
if (empty($info))
82+
return ['msg' => 'Token not found...'];
83+
84+
return $info;
85+
}
86+
}

src/Mexc/PriceTicker.php

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace WilianMaique\Mexc\Mexc;
4+
5+
/*
6+
* List symbols name
7+
* -> https://api.mexc.com/api/v3/ticker/price
8+
*/
9+
10+
class PriceTicker
11+
{
12+
public static function get(string $symbolToken = null): array|bool
13+
{
14+
$symbolToken = strtoupper($symbolToken ?? '');
15+
16+
$url = MEXC_CONFIG['MEXC_URL_API'] . '/ticker/price';
17+
$ch = curl_init($url);
18+
19+
curl_setopt_array($ch, [
20+
CURLOPT_CUSTOMREQUEST => 'GET',
21+
CURLOPT_RETURNTRANSFER => true,
22+
CURLOPT_SSL_VERIFYPEER => false,
23+
]);
24+
25+
$res = curl_exec($ch);
26+
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
27+
curl_close($ch);
28+
29+
$resJson = json_decode($res, true);
30+
31+
if ($status_code != 200)
32+
return $resJson;
33+
34+
if (empty($symbolToken))
35+
return $resJson;
36+
37+
$info = [];
38+
39+
foreach ($resJson as $key => $resJsonValue) {
40+
if ($resJsonValue['symbol'] === $symbolToken) {
41+
$info['symbol'] = $resJsonValue['symbol'];
42+
$info['price'] = $resJsonValue['price'];
43+
break;
44+
}
45+
}
46+
47+
if (empty($info))
48+
return ['msg' => 'Symbol token not found...'];
49+
50+
return $info;
51+
}
52+
}

src/Mexc/Signature.php

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace WilianMaique\Mexc\Mexc;
4+
5+
class Signature
6+
{
7+
public static function signature(array $params): string
8+
{
9+
ksort($params);
10+
return hash_hmac('sha256', http_build_query($params), MEXC_CONFIG['MEXC_API_SECRET']);
11+
}
12+
}

0 commit comments

Comments
 (0)