setOpt(CURLOPT_FOLLOWLOCATION, 5); $curl->setUserAgent('Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36'); //https 不验证ssl证书 $curl->setOpt(CURLOPT_SSL_VERIFYPEER, false); if ($timeout > 0) { $curl->setOpt(CURLOPT_TIMEOUT, $timeout); } if (is_array($headers) && !empty($headers)) { foreach ($headers as $key => $value) { $curl->setHeader($key, $value); } } $method = strtolower($method); $curl->$method($url, $params); $code = $curl->http_status_code; $response = $curl->response; $curl->close(); if ($code != 200) { throw new \App\Exceptions\ApiException($curl->error_code); } return $response; } /** * 对象重组 */ function mapByKey(array $map, $key) { $data = []; foreach ($map as $item) { if (is_array($item)) { $data[$item[$key]] = $item; } else if (is_object($item)) { $data[$item->$key] = (array)$item; } } return $data; } /** * 当前时间 * @param bool|int $ts * @return string */ function nowTime($ts = false) { return $ts ? date('Y-m-d H:i:s', $ts) : date('Y-m-d H:i:s'); } /** * 验证email格式 * @param $email * @return bool */ function validEmail($email) { $email = trim($email); $pattern = "/^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/"; if (preg_match($pattern, $email)) { return true; } else { return false; } } /** * 加密解密函数 * @param $string * @param string $operation * @param string $key * @param int $expiry * @return bool|string */ function authCode($string, $operation = 'DECODE', $key = '', $expiry = 0) { $ckey_length = 4; $key = md5($key ? $key : 'share.matchpages.com'); $keya = md5(substr($key, 0, 16)); $keyb = md5(substr($key, 16, 16)); $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length) : substr(md5(microtime()), -$ckey_length)) : ''; $cryptkey = $keya . md5($keya . $keyc); $key_length = strlen($cryptkey); $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0) . substr(md5($string . $keyb), 0, 16) . $string; $string_length = strlen($string); $result = ''; $box = range(0, 255); $rndkey = array(); for ($i = 0; $i <= 255; $i++) { $rndkey[$i] = ord($cryptkey[$i % $key_length]); } for ($j = $i = 0; $i < 256; $i++) { $j = ($j + $box[$i] + $rndkey[$i]) % 256; $tmp = $box[$i]; $box[$i] = $box[$j]; $box[$j] = $tmp; } for ($a = $j = $i = 0; $i < $string_length; $i++) { $a = ($a + 1) % 256; $j = ($j + $box[$a]) % 256; $tmp = $box[$a]; $box[$a] = $box[$j]; $box[$j] = $tmp; $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256])); } if ($operation == 'DECODE') { if ((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26) . $keyb), 0, 16)) { return substr($result, 26); } else { return ''; } } else { return $keyc . str_replace('=', '', base64_encode($result)); } } /** * html转为text * @param $str * @param int $formatText * @return mixed */ function html2text($str, $formatText = 1) { $str = preg_replace("/