264 lines
5.7 KiB
PHP
264 lines
5.7 KiB
PHP
<?php
|
||
define('USEDCHARSET', 'utf-8');
|
||
|
||
/// 接口返回用的信息类
|
||
class ResultObject
|
||
{
|
||
public $error; /// 返回值: 0成功; 非0失败;
|
||
public $error_code; /// 错误号
|
||
public $msg; /// 错误信息
|
||
public $data; /// 返回的数据
|
||
|
||
public function ResultObject($string)
|
||
{
|
||
$this->error = 0;
|
||
$this->error_code = 0;
|
||
$this->msg = null;
|
||
$this->data = null;
|
||
|
||
$this->from_string($string);
|
||
}
|
||
|
||
|
||
public function to_array()
|
||
{
|
||
return (array)$this;
|
||
}
|
||
|
||
public function to_string()
|
||
{
|
||
return json_encode($this, JSON_UNESCAPED_UNICODE);
|
||
}
|
||
|
||
public function from_array($array)
|
||
{
|
||
foreach ($array as $key => $value)
|
||
{
|
||
if (property_exists($this, $key))
|
||
{
|
||
$this->$key = $value;
|
||
}
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
public function from_string($string)
|
||
{
|
||
return $this->from_array((array)json_decode($string));
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* @note 获取当前页面的完整连接
|
||
* @return string
|
||
*/
|
||
function getLocaleUrl($always_http = false)
|
||
{
|
||
$is_https =
|
||
(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ||
|
||
(isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') ||
|
||
(isset($_SERVER['REQUEST_SCHEME']) && $_SERVER['REQUEST_SCHEME'] == 'https');
|
||
|
||
$request_scheme = $is_https ? 'https://' : 'http://';
|
||
$hostname = $_SERVER['SERVER_NAME'];
|
||
$hostport = (($is_https && '443' == $_SERVER['SERVER_PORT']) || (!$is_https && '80' == $_SERVER['SERVER_PORT'])) ? '' : ':' . intval($_SERVER['SERVER_PORT']);
|
||
|
||
if ($always_http)
|
||
return 'http://' . $hostname . $hostport;
|
||
else
|
||
return $request_scheme . $hostname . $hostport;
|
||
}
|
||
|
||
function getFullUrl($relatively_url, $always_http = false)
|
||
{
|
||
if (mb_strstr($relatively_url, '/', false, USEDCHARSET) == $relatively_url)
|
||
return getLocaleUrl($always_http) . $relatively_url;
|
||
else
|
||
return getLocaleUrl($always_http) . '/' . $relatively_url;
|
||
}
|
||
|
||
/**
|
||
* @note 获取当前页面的域名
|
||
* @return string
|
||
*/
|
||
function GetLocalRootUrl()
|
||
{
|
||
$is_https =
|
||
(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ||
|
||
(isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') ||
|
||
(isset($_SERVER['REQUEST_SCHEME']) && $_SERVER['REQUEST_SCHEME'] == 'https');
|
||
|
||
$request_scheme = $is_https ? 'https://' : 'http://';
|
||
$hostname = $_SERVER['SERVER_NAME'];
|
||
$hostport = (($is_https && '443' == $_SERVER['SERVER_PORT']) || (!$is_https && '80' == $_SERVER['SERVER_PORT'])) ? '' : ':' . intval($_SERVER['SERVER_PORT']);
|
||
|
||
return $request_scheme . $hostname . $hostport;
|
||
}
|
||
|
||
|
||
/**
|
||
* @date 2017-03-04
|
||
* @note 给参数按key的顺序排序。(支持递归)
|
||
* @param mixed $parameter 要排序的参数
|
||
* @return array
|
||
* @auth 应俊
|
||
*/
|
||
function SortParam($parameter)
|
||
{
|
||
$parameter = (array)$parameter;
|
||
foreach ($parameter as $k => $v)
|
||
{
|
||
if (is_array($v) || is_object($v))
|
||
{
|
||
$parameter[$k] = SortParam($v);
|
||
}
|
||
}
|
||
|
||
// 调用strcmp函数来排序,该函数区分大小写。
|
||
uksort($parameter, 'strcmp');
|
||
return $parameter;
|
||
}
|
||
|
||
|
||
/**
|
||
* @date 2017-03-06
|
||
* @note 转换参数成字符串形式按key=value的形式,用&分隔)。
|
||
* @param mixed $parameter 要转换的参数
|
||
* @return string
|
||
* @auth 应俊
|
||
*/
|
||
function ConvertParam($parameter)
|
||
{
|
||
$parameter = (array)$parameter;
|
||
$return = '';
|
||
foreach ($parameter as $k => $v)
|
||
{
|
||
if (is_array($v) || is_object($v))
|
||
{
|
||
$return .= sprintf('&%s={%s}', $k, ConvertParam($v));
|
||
}
|
||
else
|
||
{
|
||
$return .= sprintf('&%s=%s', $k, $v);
|
||
}
|
||
}
|
||
|
||
$sublen = mb_strlen('&', USEDCHARSET);
|
||
$retlen = mb_strlen($return, USEDCHARSET);
|
||
$return = mb_substr($return, $sublen, $retlen - $sublen, USEDCHARSET);
|
||
return $return;
|
||
}
|
||
|
||
|
||
/**
|
||
* @date 2017-03-04
|
||
* @note 为参数生成签名
|
||
* @param mixed $parameter 要签名的参数
|
||
* @param string $signkey 签名key
|
||
* @return string
|
||
* @auth 应俊
|
||
*/
|
||
function SignParameter($parameter, $signkey = '')
|
||
{
|
||
// 1:先把参数按参数名(key)从小到大排序
|
||
$parameter = SortParam($parameter);
|
||
|
||
// 2:连接参数成一个字符串(按key=value的形式,用&分隔)。
|
||
$return = ConvertParam($parameter);
|
||
|
||
// 3:结尾加上key=签名key
|
||
$return .= '&key=' . $signkey;
|
||
|
||
// 4:md5加密这个字符串
|
||
return md5($return);
|
||
}
|
||
|
||
|
||
/**
|
||
* @param mixed $data
|
||
* @return array|mixed
|
||
*/
|
||
function ChangePostData($data)
|
||
{
|
||
switch (gettype($data))
|
||
{
|
||
case 'array':
|
||
{
|
||
foreach ($data as $key => $value)
|
||
{
|
||
$data[$key] = ChangePostData($value);
|
||
}
|
||
break;
|
||
}
|
||
|
||
case 'object':
|
||
{
|
||
$array = (array)$data;
|
||
foreach ($array as $key => $value)
|
||
{
|
||
$data->$key = ChangePostData($value);
|
||
}
|
||
break;
|
||
}
|
||
|
||
default:
|
||
{
|
||
$data = preg_replace_callback('/\+/', function ($r) { return '%2B'; }, $data);
|
||
$data = preg_replace_callback('/\&/', function ($r) { return '%26'; }, $data);
|
||
break;
|
||
}
|
||
}
|
||
|
||
return $data;
|
||
}
|
||
|
||
|
||
/**
|
||
* @note 发送post请求
|
||
* @param string $url
|
||
* @param mixed $data
|
||
* @return string
|
||
*/
|
||
function SendPost($url, $data)
|
||
{
|
||
$data = http_build_query(ChangePostData($data));
|
||
|
||
$opts = array(
|
||
'http' => array(
|
||
'method' => 'POST',
|
||
'header' => "Content-type: application/x-www-form-urlencoded\r\n" . "Content-Length: " . strlen($data) . "\r\n",
|
||
'content' => $data,
|
||
),
|
||
);
|
||
|
||
$context = stream_context_create($opts);
|
||
$ret = file_get_contents($url, false, $context);
|
||
$ret = trim($ret, "\xEF\xBB\xBF");
|
||
return $ret;
|
||
}
|
||
|
||
|
||
|
||
/**
|
||
* @note 从参数中过滤不需要的参数
|
||
* @param array|null $ignores 要过滤的参数列表(参数名)
|
||
* @param array|null $parameters 目标参数列表
|
||
* @return array
|
||
*/
|
||
function GetAttachParameters($ignores = null, $parameters = null)
|
||
{
|
||
$return = (array)(empty($parameters) ? $_REQUEST : $parameters);
|
||
if (!empty($ignores))
|
||
{
|
||
foreach ($return as $k => $v)
|
||
{
|
||
if (in_array($k, $ignores))
|
||
unset($return[$k]);
|
||
}
|
||
}
|
||
|
||
return $return;
|
||
}
|