<?php
/*
*跑街令CRMAPI
*/
class LibCrmApi
{
	protected $config = null;

	function __construct()
	{
		$this->config = mvc::$cfg['CRMAPI'];
	}
	
	/**
	  * @method 发送短信
	  * @http  POST
	  * @param  $url_path           string [必填] action/method
	  * @param  $get_arr            array  [可选] ['name'=>'soul',....]
	  * @param  $post_arr           array  [可选] ['name'=>'soul',....]
	  * @param  $onlyRes            bool   [可选] 直接返回原始结果？
	  * @author soul
	  * @copyright 2017/4/13
	  * @return array
	  */
	public function Get($url_path, $get_arr=[], $post_arr=[], $onlyRes = false)
	{
		$url = $this->config['base_url'].$url_path;
		$get_arr['devcode'] =  $this->config['devcode'];
		$get_arr['from'] =  $this->config['from'];
		$get_arr['token'] =  $this->config['token'];
		$url = $url.'?'.http_build_query($get_arr);
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_TIMEOUT, 5000);
		curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
		if(!empty($post_arr))
		{
			curl_setopt($ch, CURLOPT_POST, true);
			curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_arr));
		}
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		$ssl = substr($url, 0, 8) == "https://" ? true : false;
        if($ssl)
		{
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);// 信任任何证书
			curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);// 检查证书中是否设置域名
        }
		$html = curl_exec($ch);
		curl_close($ch);
		//处理错误提示
		$return = json_decode($html, true);
		
		if(is_array($return))
		{
			if($return['encry'] == 1)
			{
				$LibEncry = new LibEncry();
				$return['data'] = $LibEncry->decode($return['data']);
				$tdata = json_decode($return['data'], true);
				if(is_array($tdata)){
					$return['data'] = $tdata;
				}
			}
			if($onlyRes) return $return;
			return LibFc::ReturnData($return['status'], $return['data'], $return['code']);
		}
		else
		{
			return LibFc::ReturnData(false, $html);
		}
	}
}