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

184 lines
3.6 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) {
2020-11-18 11:19:05 +08:00
if (is_array($val)) {
2020-11-18 11:23:23 +08:00
$xml .= "<" . $key . ">" . static::xmlChild($val) . "</" . $key . ">";
2020-11-18 11:19:05 +08:00
continue;
}
2019-07-12 19:25:47 +08:00
if (is_numeric($val)) {
$xml .= "<" . $key . ">" . $val . "</" . $key . ">";
} else {
$xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
}
}
$xml .= "</xml>";
return $xml;
}
2020-11-18 11:19:05 +08:00
/**
* @param array $array
* @return string
*/
private static function xmlChild(array $array)
{
2021-04-06 15:31:18 +08:00
$string = '';
2020-11-18 11:19:05 +08:00
foreach ($array as $key => $value) {
if (is_array($value)) {
2021-04-06 15:31:18 +08:00
$string .= static::xmlChild($value);
2020-11-18 11:19:05 +08:00
continue;
}
if (is_numeric($value)) {
2021-04-06 15:31:18 +08:00
$string .= "<" . $key . ">" . $value . "</" . $key . ">";
2020-11-18 11:19:05 +08:00
} else {
2021-04-06 15:31:18 +08:00
$string .= "<" . $key . "><![CDATA[" . $value . "]]></" . $key . ">";
2020-11-18 11:19:05 +08:00
}
}
return $string;
}
2019-07-12 19:25:47 +08:00
/**
* @param $xml
* @return mixed
*/
public static function toArray($xml)
2020-03-10 16:09:59 +08:00
{
if (is_array($xml)) {
return $xml;
}
2021-04-06 16:41:02 +08:00
/* $matchQoute = '/(<\?xml.*?\?>)?<([a-zA-Z_]+)>(<([a-zA-Z_]+)><!.*?><\/\4>)+<\/\2>/';*/
// if (!preg_match($matchQoute, $xml)) {
// return self::jsonToArray($xml);
// }
if (!is_null($data = json_decode($xml, true))) {
return $data;
2020-03-10 16:17:57 +08:00
}
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
}
2020-04-30 22:09:08 +08:00
/**
* @param mixed $json
* @return false|mixed|string
*/
public static function toJson($json)
{
if (is_object($json)) {
$json = get_object_vars($json);
}
if (is_array($json)) {
return json_encode($json, JSON_UNESCAPED_UNICODE);
}
$matchQuote = '/(<\?xml.*?\?>)?<([a-zA-Z_]+)>(<([a-zA-Z_]+)><!.*?><\/\4>)+<\/\2>/';
if (preg_match($matchQuote, $json)) {
$json = self::xmlToArray($json);
} else {
$json = json_decode($json, true);
}
if (!is_array($json)) {
$json = [];
}
return json_encode($json, JSON_UNESCAPED_UNICODE);
}
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)
{
2021-04-06 16:33:49 +08:00
ksort($array);
$buff = "";
foreach ($array as $k => $v) {
if ($k != "sign" && $v != "" && !is_array($v)) {
$buff .= $k . "=" . $v . "&";
2019-08-29 17:54:51 +08:00
}
}
2021-04-06 16:33:49 +08:00
$string = trim($buff, "&") . '&key=' . $key;
if (strtoupper($type) == 'MD5') {
2019-07-12 19:25:47 +08:00
return strtoupper(md5($string));
} else {
2021-04-06 16:33:49 +08:00
return hash_hmac('sha256', $string, $key);
2019-07-12 19:25:47 +08:00
}
}
}