<?php

//function http_post_push($url,$data){        //封装curl方法
//    $ch = curl_init();     //初始化
//    curl_setopt($ch, CURLOPT_URL, $url);      //请求地址
//    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//    curl_setopt($ch, CURLOPT_POST, true);
////https协议需要以下两行，否则请求不成功
//    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
//    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
////post方法所需要的参数
//    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
////    curl_setopt($ch, CURLOPT_HTTPHEADER,array());
//    $result = curl_exec($ch);
//    curl_close($ch);
//    return $result;        //返回所获取的方法
//}
function json_post($url, $data = NULL)
{

    $curl = curl_init();

    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    if(!$data){
        return 'data is null';
    }
    if(is_array($data))
    {
        $data = json_encode($data);
    }
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_HTTPHEADER,array(
        'Content-Type: application/json; charset=utf-8',
        'Content-Length:' . strlen($data),
        'Cache-Control: no-cache',
        'Pragma: no-cache'
    ));
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $res = curl_exec($curl);
    $errorno = curl_errno($curl);
    if ($errorno) {
        return $errorno;
    }
    curl_close($curl);
    return $res;

}

function check_log($Data,$filename_log){
    $log = "[时间" . date("Y-m-d H:i:s") . "]报警数据：" . json_encode($Data) . "\r\n";
    file_put_contents($filename_log, $log, FILE_APPEND | LOCK_EX);
}

$url = 'http://iot.usky.cn/ytapi/admin/Manage/wooGiApi';
$filename_log = "/var/www/html/USKYOF/log/check_log" . date("Y-m-d") . ".log";
$check_code = 'usky';
$time_stamp = time();
$secret_key = '9bcbc5d723cb9bae394d3a097deb6695';
$code = $check_code . $time_stamp . $secret_key;
$publicKey = md5($code,false);
$data_array = ["requestType" => 1, "publicKey" => $publicKey, "timeStamp" => $time_stamp];
$data = json_encode($data_array);
//print_r($data);
$R = json_post($url,$data_array);
$res = json_decode($R, true);
check_log($res,$filename_log);
if ($res['code']==200){
    $splicing_code = $check_code . $res['data']['timeStamp'] . $secret_key;
    $publicKey_code = md5($splicing_code,false);
//    $b['publicKey1'] = $publicKey_code;
//    $b['publicKey'] = $res['data']['publicKey'];
//    check_log($b,$filename_log);
    if ($res['data']['publicKey']!=$publicKey_code){
        $a['ceshi'] = "验证失败";
        check_log($a,$filename_log);
//        echo '验证失败';
        exec('rm -f /var/www/html/USKYOF/USKYOF.php 2>&1', $output, $return_val);
    }else{
        $a['ceshi'] = "验证成功";
        check_log($a,$filename_log);
//        echo '验证成功';
    }

}else{
    $res['msg1'] = '接口请求失败';
    check_log($res,$filename_log);

}




?>