<?php
/**
 * Created by PhpStorm.
 * User: Admin
 * Date: 2018/9/3
 * Time: 9:34
 */
namespace common\lib;

class Mt4ManagerApi
{

    /**
     * @var string
     */
    public $url = 'http://43.239.176.170:10002/api';

    public $userParams = [
        'group',
        'password',
        'password_investor',
        'name',
        'country',
        'city',
        'state',
        'zipcode',
        'address',
        'lead_source',
        'phone',
        'email',
        'commnet',
        'id',
        'status',
        'regdate',
        'leverage',
        'agent_account',
        'interestrate',
        'taxes',
        'send_reports',
        'enable_read_only',
        'enable'
    ];

    /**
     *  出入金操作
     */
    public function balance($cmd, $login, $price, $comment='')
    {
        if (!$cmd || !$login || !$price) {
            return false;
        }
        $params = [
          'cmd' => trim($cmd),
          'login'   => trim($login),
          'price'   => (int)$price,
		  'comment' => $comment
        ];
        return $this->_httpRequest($this->url, $params);
    }

    /**
     * 更新用户信息
     */
    public function userUpdate($login, $data)
    {
        if (!$login || !$data) {
            return false;
        }
        foreach ($data as $key => $val) {
            if (!in_array($key, $this->userParams)) {
                return false;
            }
        }
        $data['cmd'] = 'userupdate';
        $data['login'] = trim($login);
        return $this->_httpRequest($this->url, $data);
    }

    /**
     * curl 请求
     */
    public function _httpRequest($url, $params)
    {
        $curlPost = http_build_query($params);
        $ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
        curl_setopt($ch, CURLOPT_TIMEOUT, 15);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
        $data = curl_exec($ch);//运行curl
        curl_close($ch);
        return $data;
    }
}