php对接开关demo

这是一个php版本的demo,用于对接开关的API接口,可以控制开关的开和关,还有获取开关的状态

数据传输过程使用discuz加解密方法,个人密匙可以自己配置

1.demo文件

<?php
	include('include/AuthCode.class.php');
	$key = "d0d3d5b5466cc1ee89eb808de9f5672d";//个人密匙
	$mid = "5b53a2d341d78427feafeb4520bf41eb";//设备密匙
	$url = "https://wx663ca7ac2632b137.vip.aoyacms.com/plugin/aoya/iot/api.php";//接口地址
	$t = time();//获取时间戳
	//控制开关
	$info = array(
		"act" => "switch"
	);
	$json = json_encode($info);//将操作转成json格式
	$ciphertext = AuthCode::encode($json,$key);//密文
	$check = md5($t.$mid.$ciphertext);//md5验证
	//获取开关状态
	$info2 = array(
		"act" => "getState"	
	);
	$json2 = json_encode($info2);
	$ciphertext2 = AuthCode::encode($json2,$key);//密文
	$check2 = md5($t.$mid.$ciphertext2);//md5验证

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>测试</title>
</head>

<body>
    <form action="<?php echo $url;?>" method="post" >
        <input type="hidden" name="ciphertext" value="<?php echo $ciphertext?>"  />
        <input type="hidden" name="t" value="<?php echo $t?>"  />
        <input type="hidden" name="check" value="<?php echo $check?>"  />
        <input type="hidden" name="mid" value="<?php echo $mid?>"  />
        <input type="submit" value="开/关"  />
    </form>
    <form action="<?php echo $url;?>" method="post"  style="margin-top:20px;">
        <input type="hidden" name="ciphertext" value="<?php echo $ciphertext2?>"  />
        <input type="hidden" name="t" value="<?php echo $t?>"  />
        <input type="hidden" name="check" value="<?php echo $check2?>"  />
        <input type="hidden" name="mid" value="<?php echo $mid?>"  />
        <input type="submit" value="获取开关状态"  />
    </form>
</body>
</html>
2.加解密类
<?php
	class AuthCode {
		public static function encode($str, $key) {
			return self::_auth_code($str, 'ENCODE', $key, 0);
		}
	 
		public static function decode($str, $key) {
			return self::_auth_code($str, 'DECODE', $key, 0);
		}
	 
		public static function _auth_code($string, $operation = 'DECODE', $key = '', $expiry = 3600) {
			/***
			 * 随机密钥长度 取值 0-32;
			 * 加入随机密钥,可以令密文无任何规律,即便是原文和密钥完全相同,加密结果也会每次不同,增大破解难度。
			 * 取值越大,密文变动规律越大,密文变化 = 16 的 $ckey_length 次方
			 * 当此值为 0 时,则不产生随机密钥
			 */
			$ckey_length = 4;
			
			$key = md5($key);
			$keya = md5(substr($key, 0, 16));
			$keyb = md5(substr($key, 16, 16));
			$keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';
	 
			$cryptkey = $keya.md5($keya.$keyc);
			$key_length = strlen($cryptkey);
	 
			$string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
			$string_length = strlen($string);
	 
			$result = '';
			$box = range(0, 255);
	 
			$rndkey = array();
			for($i = 0; $i <= 255; $i++) {
				$rndkey[$i] = ord($cryptkey[$i % $key_length]);
			}
	 
			for($j = $i = 0; $i < 256; $i++) {
				$j = ($j + $box[$i] + $rndkey[$i]) % 256;
				$tmp = $box[$i];
				$box[$i] = $box[$j];
				$box[$j] = $tmp;
			}
	 
			for($a = $j = $i = 0; $i < $string_length; $i++) {
				$a = ($a + 1) % 256;
				$j = ($j + $box[$a]) % 256;
				$tmp = $box[$a];
				$box[$a] = $box[$j];
				$box[$j] = $tmp;
				$result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
			}
	 
			if($operation == 'DECODE') {
				if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
					return substr($result, 26);
				} else {
					return '';
				}
			} else {
				return $keyc.base64_encode($result);
			}
		}
	 
	}
?>

傲亚物联网

物联网

码圣代码编程

打字练习

3D创意C++

进入比赛

创客中心

关于傲亚

傲亚CMS

傲亚物联网

我要合作

创客平台

南昌市青山湖区恒茂梦时代7栋2303
aoyakefu
TEL:18720086320
kefu@1wwz.com

扫码关注公众号

扫码添加创始人

企业微信服务商