Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

/open/api/v2/order/cancel #5

Open
engcelik opened this issue Feb 14, 2022 · 21 comments
Open

/open/api/v2/order/cancel #5

engcelik opened this issue Feb 14, 2022 · 21 comments

Comments

@engcelik
Copy link

[msg] => authorize failed

  | [code] => 401
  | [_method] => DELETE
  | [_url] => https://www.mexc.com//open/api/v2/order/cancel
  | [_httpcode] => 400

help me :(

@zhouaini528
Copy link
Owner

zhouaini528 commented Feb 15, 2022

[msg] => authorize failed

  | [code] => 401   | [_method] => DELETE   | [_url] => https://www.mexc.com//open/api/v2/order/cancel   | [_httpcode] => 400

help me :(

It's solved, try again.
You don't need to pass the parameter $host, there is a default address.

@engcelik
Copy link
Author

function MEXC_Order_DeleteCancel($db,$key,$secret,$symbol,$order_ids){
$mxc=new MxcSpot($key,$secret);
$mxc->setOptions(['timeout'=>10,]);
try {
$result=$mxc->order()->deleteCancel([
'symbol'=>$symbol,
'order_ids'=>$order_ids,
]);
print_r($result);

}catch (\Exception $e){
print_r(json_decode($e->getMessage(),true));
}
sleep(1);
}

@engcelik
Copy link
Author

function __construct(string $key='',string $secret='',string $host='https://www.mexc.com'){
    $this->key=$key;
    $this->secret=$secret;
    $this->host=$host;
}

@engcelik
Copy link
Author

xamp Windows apache

@zhouaini528
Copy link
Owner

xamp Windows apache

it has to run under linux.

@zhouaini528
Copy link
Owner

function __construct(string $key='',string $secret='',string $host='https://www.mexc.com'){
    $this->key=$key;
    $this->secret=$secret;
    $this->host=$host;
}

I don't understand what the code means.

@engcelik
Copy link
Author

image

@engcelik
Copy link
Author

default host address

@zhouaini528
Copy link
Owner

default host address

ok. Is it still authorize failed error?

@engcelik
Copy link
Author


Array
(
[code] => 200
[data] => Array
(
[0] => Array
(
[id] => 4a36cea324ee4711a46c728097df8a06
[symbol] => DXGM_USDT
[price] => 6
[quantity] => 1
[state] => NEW
[type] => ASK
[remain_quantity] => 1
[remain_amount] => 6
[create_time] => 1644914994187
[client_order_id] =>
[order_type] => LIMIT_ORDER
)

[1] => Array
(
[id] => d96f5ee9629a40a1bef4a48a29ec1685
[symbol] => DXGM_USDT
[price] => 6
[quantity] => 1
[state] => NEW
[type] => ASK
[remain_quantity] => 1
[remain_amount] => 6
[create_time] => 1644914351934
[client_order_id] =>
[order_type] => LIMIT_ORDER
)

)

)
Array
(
[msg] => authorize failed
[code] => 401
[_method] => DELETE
[_url] => https://www.mexc.com/open/api/v2/order/cancel
[_httpcode] => 400
)

@engcelik
Copy link
Author

There is an error
only when you delete

@engcelik
Copy link
Author

image

@zhouaini528
Copy link
Owner

zhouaini528 commented Feb 15, 2022

There is an error only when you delete

$mexc=new MxcSpot($key,$secret);

//You can set special needs
$mexc->setOptions([
    //Set the request timeout to 60 seconds by default
    'timeout'=>10,
]);

//Place an Order
try {
    $result=$mexc->order()->postPlace([
        'symbol'=>'EOS_USDT',
        'price'=>'6',
        'quantity'=>1,
        'trade_type'=>'ASK',//BID=buy,ASK=sell
        'order_type'=>'LIMIT_ORDER',//LIMIT_ORDER,POST_ONLY,IMMEDIATE_OR_CANCEL
        //'client_order_id'=>''
        
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r(json_decode($e->getMessage(),true));
} 
sleep(1);

//Get order details by order ID.
try {
    $result=$mexc->order()->getQuery([
        'symbol'=>'EOS_USDT',
        'order_ids'=>$result['data'],
        //'client_order_ids'=>'',
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r(json_decode($e->getMessage(),true));
}
sleep(1); 

//Cancelling an unfilled order.
try {
    $result=$mexc->order()->deleteCancel([
        'symbol'=>'EOS_USDT',
        'order_ids'=>$result['data'][0]['id'],
        //'client_order_ids'=>'',
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r(json_decode($e->getMessage(),true));
}

(https://github.com/zhouaini528/mxc-php/blob/master/tests/spot/order.php#L38)[https://github.com/zhouaini528/mxc-php/blob/master/tests/spot/order.php#L38]

Array
(
    [code] => 200
    [data] => 41497759ef5749b588efa0523f6dbede
)
Array
(
    [code] => 200
    [data] => Array
        (
            [0] => Array
                (
                    [id] => 41497759ef5749b588efa0523f6dbede
                    [symbol] => EOS_USDT
                    [price] => 6
                    [quantity] => 1
                    [state] => NEW
                    [type] => ASK
                    [deal_quantity] => 0
                    [deal_amount] => 0
                    [create_time] => 1644915514000
                    [order_type] => LIMIT_ORDER
                )

        )

)
Array
(
    [code] => 200
    [data] => Array
        (
            [41497759ef5749b588efa0523f6dbede] => success
        )

)

I tested and found no problems, are you using the latest version?

@zhouaini528
Copy link
Owner

zhouaini528 commented Feb 15, 2022

2022-02-15 17-05-11屏幕截图
2022-02-15 17-05-11

@engcelik
Copy link
Author

  Array
  (
  [code] => 200
  [data] => 9a4e784a18a946a4b7f2092a365dce20
  )
  Array
  (
  [code] => 200
  [data] => Array
  (
  [0] => Array
  (
  [id] => 9a4e784a18a946a4b7f2092a365dce20
  [symbol] => DXGM_USDT
  [price] => 6
  [quantity] => 1
  [state] => NEW
  [type] => ASK
  [deal_quantity] => 0
  [deal_amount] => 0
  [create_time] => 1644916280000
  [order_type] => LIMIT_ORDER
  )
   
  )
   
  )
  Array
  (
  [msg] => authorize failed
  [code] => 401
  [_method] => DELETE
  [_url] => https://www.mexc.com/open/api/v2/order/cancel
  [_httpcode] => 400
  )

@engcelik
Copy link
Author

the code you sent

@zhouaini528
Copy link
Owner

the code you sent

very strange question, I'll look it up again

@zhouaini528
Copy link
Owner

@engcelik
Copy link
Author

Updated the last code you posted. Problem is solved .

thanks 👍

@engcelik
Copy link
Author

Request.php upload

@engcelik
Copy link
Author

* */ namespace Lin\Mxc; use GuzzleHttp\Exception\RequestException; use Lin\Mxc\Exceptions\Exception; class Request { protected $key=''; protected $secret=''; protected $host=''; protected $nonce=''; protected $signature=''; protected $headers=[]; protected $type=''; protected $path=''; protected $data=[]; protected $options=[]; protected $authentication=true; protected $platform=''; protected $version=''; public function __construct(array $data) { $this->key=$data['key'] ?? ''; $this->secret=$data['secret'] ?? ''; $this->host=$data['host'] ?? 'https://www.mexc.com/'; $this->options=$data['options'] ?? []; $this->platform=$data['platform'] ?? []; $this->version=$data['version'] ?? []; } /** * * */ protected function auth(){ $this->nonce(); $this->signature(); $this->headers(); $this->options(); } /** * * */ protected function nonce(){ $this->nonce=time()*1000; } /** * * */ protected function signature(){ if($this->authentication==false) return; if(in_array($this->type,['GET','DELETE'])) { $params= empty($this->data) ? '' : implode('&',$this->sort($this->data)); }else{ $params= empty($this->data) ? '' : json_encode($this->data); } //accessKey+时间戳+获取到的参数字符串 $what = $this->key . $this->nonce . $params; //echo $what.PHP_EOL; $this->signature = hash_hmac("sha256", $what, $this->secret); } /** * * */ protected function headers(){ $this->headers=[ 'Content-Type' => 'application/json', 'ApiKey' => $this->key, 'Request-Time'=>$this->nonce, 'Signature'=>$this->signature, ]; } /** * * */ protected function options(){ if(isset($this->options['headers'])) $this->headers=array_merge($this->headers,$this->options['headers']); $this->options['headers']=$this->headers; $this->options['timeout'] = $this->options['timeout'] ?? 60; } /** * * */ protected function send(){ $client = new \GuzzleHttp\Client(); $url=$this->host.$this->path; if(in_array($this->type,['GET','DELETE'])) $url.= empty($this->data) ? '' : '?'.http_build_query($this->data); else $this->options['body']=json_encode($this->data); // echo $url.PHP_EOL; // print_r($this->options); // die; $response = $client->request($this->type, $url, $this->options); return $response->getBody()->getContents(); } /* * * */ protected function exec(){ $this->auth(); try { return json_decode($this->send(),true); }catch (RequestException $e){ if(method_exists($e->getResponse(),'getBody')){ $contents=$e->getResponse()->getBody()->getContents(); $temp=json_decode($contents,true); if(!empty($temp)) { $temp['_method']=$this->type; $temp['_url']=$this->host.$this->path; }else{ $temp['_message']=$e->getMessage(); } }else{ $temp['_message']=$e->getMessage(); } $temp['_httpcode']=$e->getCode(); throw new Exception(json_encode($temp)); } } /** * * */ protected function sort($param) { $u = []; $sort_rank = []; foreach ($param as $k => $v) { $u[] = $k . "=" . urlencode($v); $sort_rank[] = ord($k); } asort($u); return $u; } } ?>

@engcelik engcelik reopened this Feb 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants