<?php

//调用下单
(new demo())->order();

/**
 * Class demo
 * demo 开放平台下单
 */
class demo
{
    /**
     * @var string 接口地址 测试地址
     */
	static $serverUrl = "http://devkyweixin.yundasys.com/openapi-api/v1/order/pushOrder";
    /**
     * @var string 创建应用分配的appkey
     */
	static $appKey = "000083";
    /**
     * @var string 创建应用分配的appsecret
     */
	static $appSecret = "ea1ac6ce075111eab0af139563344bd6";
    /**
     * 下单
     */
	public static function order(){
	    //下单报文
		$info = [
			'appid'=>"999999",
			'backparam'=>"测试",
			'backurl'=>"http://www",
			'freight'=>10,
			'items'=>[
				[
					'name'=>'衣服',
					'number'=>1,
					'remark'=>'袜子',
				]
			],
			'orderid'=>"2333098766",
			'other_charges'=>0,
			'premium'=>1,
			'receiver'=>[
				'address'=>'青浦区盈港东路 6679 号',
				'city'=>'上海市',
				'mobile'=>'0553-9876542',
				'name'=>'李四',
				'phone'=>'17601205970',
				'county'=>'青浦区',
				'company'=>'string',
				'postcode'=>'string',
				'province'=>'上海市',
			],
			'remark'=>"string",
			'sendendtime'=>"2019-09-03 11:00:00",
			'sender'=>[
				'address'=>'青浦区盈港东路 6679 号',
				'city'=>'上海市',
				'mobile'=>'0553-9876542',
				'name'=>'李四',
				'phone'=>'17601205970',
				'county'=>'青浦区',
				'company'=>'string',
				'postcode'=>'string',
				'province'=>'上海市',
			],		
			'sendstarttime'=>"2019-09-03 10:00:00",
			'size'=>"0.12,0.23,0.11",
			'special'=>0,
			'value'=>126.5,
			'weight'=>0,
		];
        //请求参数body,指定JSON格式   转json
		$json_info = json_encode($info,JSON_UNESCAPED_UNICODE);
		//生成的SIGN签名串
		$sign = md5($json_info.'_'.self::$appSecret);
		//header
		$header = [
			'app-key:'.self::$appKey,
			'sign:'.$sign,
			'req-time:'.time(),
			'Content-Type:application/json;charset=UTF-8',
		];
		//http请求
        try {
            $res = (new Http())->postJson(self::$serverUrl, $json_info, $header);
            if (!empty($res)){
                $res_array = json_decode($res,true);
                if ($res_array['code'] === '0000'){
                    echo '请求成功'.PHP_EOL;
                }else{
                    echo '请求失败'.self::getResCodeInfo($res_array['code']).PHP_EOL;
                }
                echo $res.PHP_EOL;
            }else{
                echo '请求失败：无返回';
            }
        }catch (Exception $e){
            echo '请求失败：'.$e->getMessage();
        }
	}

    /**
     * 返回报文code对应关系
     * @param $code
     * @return string
     */
	private static function getResCodeInfo($code){
	    switch ($code){
            case '0000':
                return "请求成功";
            case '7100':
                return "账号无权限";
            case '7200':
                return "接口无权限";
            case '7300':
                return "IP无权限";
            case '7400':
                return "签名失败";
            case '7500':
                return "超过单用户日最高访问量";
            case '7501':
                return "超过日访问量最高值";
            case '7502':
                return "超过单用户接口QPS最大限制";
            case '7503':
                return "超过该接口QPS最大限制";
            case '7600':
                return "头信息header参数中缺少app-key";
            case '7601':
                return "头信息header参数中缺少sign";
            case '7602':
                return "头信息header参数中缺少req-time";
            case '7603':
                return "content-type只支持application/json;utf-8格式";
            case '7604':
                return "httpmothod只支持post类型";
            case '7605':
                return "请求body参数不能为空";
            case '7777':
                return "内部服务错误";
            default:
                return '未知错误';
        }
    }
}

/**
 * http
 * Class Http
 */
class Http
{
    /**
     * http 请求
     * @param $url
     * @param $data
     * @param array $header
     * @param int $timeout
     * @return mixed|string
     * @throws Exception
     */
    public static function postJson($url, $data, $header = array(
        "Content-Type: application/json;charset=UTF-8"
    ),$timeout=5)
    {
        $url = trim($url);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $res = curl_exec($ch);
        if ($res === false) {
        	$res = curl_error($ch);
        	throw new Exception($res, 1);
        }
        curl_close($ch);
        return $res;
    }
}
