<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
function flag_options()
{
    return ['a' => 'a', 'b' => 'b'];
}

function post_json_data($url, $data_string)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json; charset=utf-8',
            'Content-Length: ' . strlen($data_string))
    );
    ob_start();
    curl_exec($ch);
    $return_content = ob_get_contents();
    ob_end_clean();
    $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    return array('code' => $return_code, 'result' => $return_content);
}

$arr = array('a' => '555', 'b' => 56454564);
//dump(post_json_data('http://192.168.211.1/html/dump.php',json_encode($arr)));


/**
 * curl
 *
 * @param
 *            string url
 * @param
 *            array 数据
 * @param
 *            int 请求超时时间
 * @param
 *            bool HTTPS时是否进行严格认证
 * @return string
 */
function curl_post_json($url, $data,$timeout=100){

    $CA = false;

    // $url = "http://www.baidu.com";
    $cacert = getcwd() . '/cacert.pem'; // CA根证书
    $SSL    = substr($url, 0, 8) == "https://" ? true : false;

    $ch     = curl_init();
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    //var_dump($data);exit;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout - 2);
    //curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727;cli-test)');
    if ($SSL && $CA) {
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); // 只信任CA颁布的证书
        curl_setopt($ch, CURLOPT_CAINFO, $cacert); // CA根证书（用来验证的网站证书是否是CA颁布）
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // 检查证书中是否设置域名，并且是否与提供的主机名匹配
    } else if ($SSL && !$CA) {
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 信任任何证书
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 检查证书中是否设置域名
    }
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //不输出内容到页面
//    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
//        'Expect:'
//    ));
    // var_dump($data);

    date_default_timezone_set("UTC");
    $nonce          = '66C92B11FF8A425FB8D4CCFE';
    //$nonce = $nonce1          = mt_rand(100000,9999999999);
    $time           = time();
    $created        = date('Y-m-d', $time) . 'T' . date('H:i:s', $time) . 'Z';
    //$created        = '2019-07-10T15:02:08Z';
    $password       = '33ab0a75ee246f85e41f2c15d867ca5d';
    $nonce = base64_encode($nonce);// . $created . $password;
    $signRawString = $nonce . $created . $password;//;//
//    echo "明文:", $signRawString,"\n";
//    echo "明文:", $signRawString,"\n";
    //$signRawString = '66C92B11FF8A425FB8D4CCFE2019-07-08T17:56:46Z33ab0a75ee246f85e41f2c15d867ca5d';
    //$signRawString='a';
    //var_dump(hash('sha256',$signRawString,true));exit;
    $passwordDigest = base64_encode(hash('sha256',$signRawString,true));
    
    //echo "明文: $signRawString \n";
    //echo "密文: $passwordDigest \n";
    //exit;


    $headerArr[] = 'Authorization: WSSE realm="SDP", profile="UsernameToken", type="Appkey"';
    $headerArr[] = 'X-WSSE:UsernameToken Username="d9c3190120db4b2ab078a543d59ba47d",PasswordDigest="' . $passwordDigest . '",Nonce="' . $nonce . '",Created="' . $created . '"';
    $headerArr[] = 'Content-Type: application/json; charset=UTF-8';
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArr);
    //var_dump(C('test_proxy'));exit;
    //if(C('test_proxy')) {
    //curl_setopt($ch, CURLOPT_PROXY, '192.168.1.211:8888');
    //}
    $ret = curl_exec($ch);
    if (empty($ret)) {
        var_dump(curl_error($ch)); // 查看报错信息
    }
    // exit('x');
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    if ($httpCode != 200) {

    }
    curl_close($ch);
    //var_dump($ret);
    return $ret;
}

function curl_post_json_pay($url, $data,$headers=[],$timeout=100){


    $CA = false;

    $cacert = getcwd() . '/cacert.pem'; // CA根证书
    $SSL    = substr($url, 0, 8) == "https://" ? true : false;

    $ch     = curl_init();
    curl_setopt($ch, CURLOPT_POST, true);

    if(!empty($data))   curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    //var_dump($data);exit;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout - 2);
    //curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727;cli-test)');
    if ($SSL && $CA) {
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); // 只信任CA颁布的证书
        curl_setopt($ch, CURLOPT_CAINFO, $cacert); // CA根证书（用来验证的网站证书是否是CA颁布）
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // 检查证书中是否设置域名，并且是否与提供的主机名匹配
    } else if ($SSL && !$CA) {
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 信任任何证书
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 检查证书中是否设置域名
    }

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //不输出内容到页面

    date_default_timezone_set("UTC");



    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    //var_dump(C('test_proxy'));exit;
    //if(C('test_proxy')) {
    //curl_setopt($ch, CURLOPT_PROXY, '192.168.16.96:8888');
    //}
    $ret = curl_exec($ch);
    if (empty($ret)) {
        var_dump(curl_error($ch)); // 查看报错信息
    }

    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
//
//    if ($httpCode != 200) {
//        $tmp              = array();
//        $tmp['http_code'] = $httpCode;
//        $tmp['data']      = $ret;
//
//        //echo "\n服务器错误：$httpCode";
//        //echo $ret;
//        $ret = json_encode($tmp);
//    }
    curl_close($ch);
    //var_dump($ret);
    return $ret;
}




/**
 * curl
 *
 * @param
 *            string url
 * @param
 *            array 数据
 * @param
 *            int 请求超时时间
 * @param
 *            bool HTTPS时是否进行严格认证
 * @return string
 */
function curl_post_json_sdk($url, $data,$timeout=100){

    $CA = false;

    // $url = "http://www.baidu.com";
    $cacert = getcwd() . '/cacert.pem'; // CA根证书
    $SSL    = substr($url, 0, 8) == "https://" ? true : false;

    $ch     = curl_init();
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    //var_dump($data);exit;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout - 2);
    //curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727;cli-test)');
    if ($SSL && $CA) {
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); // 只信任CA颁布的证书
        curl_setopt($ch, CURLOPT_CAINFO, $cacert); // CA根证书（用来验证的网站证书是否是CA颁布）
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // 检查证书中是否设置域名，并且是否与提供的主机名匹配
    } else if ($SSL && !$CA) {
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 信任任何证书
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 检查证书中是否设置域名
    }
    //curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
//        'Expect:'
//    ));
    // var_dump($data);


    $headerArr=[];

    curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArr);
    //var_dump(C('test_proxy'));exit;
    //if(C('test_proxy')) {
    curl_setopt($ch, CURLOPT_PROXY, '192.168.16.96:8888');
    //}
    $ret = curl_exec($ch);
    if (empty($ret)) {
        var_dump(curl_error($ch)); // 查看报错信息
    }
    // var_dump($ret);
    // exit('x');
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    if ($httpCode != 200) {
        $tmp              = array();
        $tmp['http_code'] = $httpCode;
        $tmp['data']      = $ret;

        //echo "\n服务器错误：$httpCode";
        //echo $ret;
        $ret = json_encode($tmp);
    }
    curl_close($ch);
    //var_dump($ret);
    return $ret;
}

function I(string $key = '', $default = null, $filter = ''){
    return input($key,$default,$filter);
}