Files
kiri-wchat/common/Help.php
T

94 lines
1.7 KiB
PHP
Raw Normal View History

2019-07-12 19:25:47 +08:00
<?php
2019-11-11 18:14:47 +08:00
namespace common;
2019-07-12 19:25:47 +08:00
2019-07-17 17:17:37 +08:00
class Help extends Miniprogarampage
2019-07-12 19:25:47 +08:00
{
2019-08-29 17:54:51 +08:00
2019-07-12 19:25:47 +08:00
/**
* @param array $data
* @return string
*/
public static function toXml(array $data)
{
$xml = "<xml>";
foreach ($data as $key => $val) {
if (is_numeric($val)) {
$xml .= "<" . $key . ">" . $val . "</" . $key . ">";
} else {
$xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
}
}
$xml .= "</xml>";
return $xml;
}
/**
* @param $xml
* @return mixed
*/
public static function toArray($xml)
{
if (!is_null($json = json_decode($xml, TRUE))) {
return $json;
}
if (is_array($json)) {
return $json;
}
$data = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
return json_decode(json_encode($data), TRUE);
}
2019-07-17 17:17:37 +08:00
/**
* @param int $length
* @return string
*
* 随机字符串
*/
public static function random($length = 20)
{
$res = [];
$str = 'abcdefghijklmnopqrstuvwxyz';
$str .= strtoupper($str) . '1234567890';
for ($i = 0; $i < $length; $i++) {
$rand = substr($str, rand(0, strlen($str) - 2), 1);
if (empty($rand)) {
$rand = substr($str, strlen($str) - 3, 1);
}
array_push($res, $rand);
}
return implode($res);
}
2019-07-12 19:25:47 +08:00
/**
* @param array $array
* @param $key
* @param $type
* @return string
*/
public static function sign(array $array, $key, $type)
{
2019-08-29 18:05:46 +08:00
ksort($array, SORT_ASC);
2019-08-29 17:54:51 +08:00
$string = [];
2019-08-29 18:05:46 +08:00
foreach ($array as $hashKey => $val) {
2019-08-29 17:54:51 +08:00
if (empty($val)) {
continue;
}
2019-08-29 18:07:11 +08:00
$string[] = $hashKey . '=' . $val;
2019-08-29 17:54:51 +08:00
}
$string[] = 'key=' . $key;
$string = implode('&', $string);
2019-07-12 19:25:47 +08:00
if ($type == 'MD5') {
return strtoupper(md5($string));
} else {
return hash('sha256', $string);
}
}
}