Files
kiri-wchat/wchat/common/Help.php
T

132 lines
2.4 KiB
PHP
Raw Normal View History

2019-07-12 19:25:47 +08:00
<?php
2020-03-05 12:41:49 +08:00
namespace wchat\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)
2020-03-10 16:09:59 +08:00
{
if (is_array($xml)) {
return $xml;
}
2020-03-10 16:17:57 +08:00
$matchQoute = '/(<\?xml.*?\?>)?<([a-zA-Z_]+)>(<([a-zA-Z_]+)><!.*?><\/\4>)+<\/\2>/';
if (!preg_match($matchQoute, $xml)) {
return self::jsonToArray($xml);
}
2020-03-10 16:09:59 +08:00
try {
$data = @simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
if ($data !== false) {
2020-03-10 16:17:57 +08:00
$data = json_decode(json_encode($data), TRUE);
} else {
$data = $xml;
2020-03-10 16:09:59 +08:00
}
} catch (\Exception $exception) {
2020-03-10 16:17:57 +08:00
$data = $xml;
2020-03-10 16:09:59 +08:00
} finally {
2020-03-10 16:17:57 +08:00
return $data;
2020-03-10 16:09:59 +08:00
}
}
2020-03-10 16:17:57 +08:00
public static function jsonToArray($xml)
{
$_xml = json_decode($xml, true);
if (is_null($_xml)) {
return $xml;
}
return $_xml;
}
2020-03-10 16:09:59 +08:00
/**
* @param $xml
* @return mixed
*/
public static function xmlToArray($xml)
2019-07-12 19:25:47 +08:00
{
2020-01-03 11:48:51 +08:00
if (is_array($xml)) {
return $xml;
}
2020-03-09 16:19:23 +08:00
if (($data = @simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)) !== false) {
return json_decode(json_encode($data), TRUE);
2019-07-12 19:25:47 +08:00
}
2020-03-09 16:19:23 +08:00
if (!is_null($json = json_decode($xml, TRUE))) {
2019-07-12 19:25:47 +08:00
return $json;
}
2020-03-09 16:19:23 +08:00
return $xml;
2020-01-09 18:57:08 +08:00
}
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);
}
}
}