123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- <?php
- /**
- * [reValue 格式化返回数据]
- * @param [type] $data [返回数据]
- * @param integer $code [返回状态码]
- * @param [array] $message [消息数组]
- * @return [string] [json字符串]
- */
- function reValue($code = 0, $data = null, $message = null)
- {
- $message = !empty($message) ? $message : \App\Support\Message::getMessage($code);
- // if (!is_array($message)) {
- // $message = [$message];
- // }
- $value = json_encode(["res" => $data, "message" => $message, "code" => $code]);
- return $value;
- }
- /**
- * [randomStr 获取随机字符串]
- * @Author mzb
- * @DateTime 2018-04-02T16:50:58+0800
- * @param integer $length [获取字符串长度]
- * @param boolean $isNum [是否获取数字类型字符串]
- * @return [type] [description]
- */
- function randomStr($length = 4, $isNum = true)
- {
- $str = '0123456789';
- if (!$isNum) {
- $str .= 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
- }
- $strLength = strlen($str);
- $length = strlen($str) >= $length ? $length : $strLength;
- $str = str_shuffle($str);
- return substr($str, 0, $length);
- }
- /**
- * [geturl curl发送get请求]
- * @Author mzb
- * @DateTime 2018-06-11T11:24:59+0800
- * @param [type] $url [description]
- * @return [type] [description]
- */
- // function geturl($url)
- // {
- // $headerArray = array("Content-type:application/json;", "Accept:application/json");
- // $ch = curl_init();
- // curl_setopt($ch, CURLOPT_URL, $url);
- // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- // curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- // curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- // curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray);
- // $output = curl_exec($ch);
- // curl_close($ch);
- // $output = json_decode($output, true);
- // return $output;
- // }
- function curlInit($url, $fields = null, $method, $headers = null, $timeout = 10)
- {
- $ch = curl_init();
- // curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8','Content-Type:application/x-www-form-urlencoded', 'charset=utf-8'));
- // curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8',
- // 'Content-Type:application/x-www-form-urlencoded', 'charset=utf-8'));
- if (isset($headers)) {
- curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
- }
- // curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- // curl_setopt($ch, CURLOPT_HEADER, true);
- // curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
- // curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- if ($method == "Post") {
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_POST, true);
- if (isset($fields)) {
- curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
- }
- } else if($method == 'json') {
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
- } else {
- curl_setopt($ch, CURLOPT_URL, $url . '?' . http_build_query($fields));
- curl_setopt($ch, CURLOPT_POST, false);
- }
- $content = curl_exec($ch);
- // var_dump($content);
- // $result = "";
- // if ($content !== false) {
- // } else {
- // $result = json_encode(array(
- // "reason" => "network error or timeout",
- // ));
- // }
- // Close connection
- curl_close($ch);
- return $content;
- }
- /*
- * 以下代码实现PHP sha256() sha256_file() sha512() sha512_file() PHP 5.1.2+完美兼容
- * @param string $data 要计算散列值的字符串
- * @param boolean $rawOutput 为true时返回原始二进制数据,否则返回字符串
- * @param string file 要计算散列值的文件名,可以是单独的文件名,也可以包含路径,绝对路径相对路径都可以
- * @return boolean | string 参数无效或者文件不存在或者文件不可读时返回false,计算成功则返回对应的散列值
- * @notes 使用示例 sha256('mrdede.com') sha512('mrdede.com') sha256_file('index.php') sha512_file('index.php')
- */
- /* PHP sha256() */
- // function sha256($data, $rawOutput = false)
- // {
- // if (!is_scalar($data)) {
- // return false;
- // }
- // $data = (string) $data;
- // $rawOutput = !!$rawOutput;
- // return hash('sha256', $data, $rawOutput);
- // }
- function getMillisecond()
- {
- list($microsecond, $time) = explode(' ', microtime()); //' '中间是一个空格
- return (float) sprintf('%.0f', (floatval($microsecond) + floatval($time)) * 1000);
- }
- function curlPost($url, $data, $timeout = 3, $headerAry = '')
- {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
- curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
- curl_setopt($ch, CURLOPT_HEADER, false);
- if ($headerAry != '') {
- curl_setopt($ch, CURLOPT_HTTPHEADER, $headerAry);
- }
- $res = curl_exec($ch);
- return $res;
- }
|