- //获取IP
- function ip()
- {
- if(getenv('X-Forwarded-For')&&strcasecmp(getenv('X-Forwarded-For'), 'unknown'))
- {
- $ip = getenv('X-Forwarded-For');
- }
- elseif(getenv('HTTP_CLIENT_IP')&&strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown'))
- {
- $ip = getenv('HTTP_CLIENT_IP');
- }
- elseif(getenv('HTTP_X_FORWARDED_FOR')&&strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown'))
- {
- $ip = getenv('HTTP_X_FORWARDED_FOR');
- }
- elseif(getenv('REMOTE_ADDR')&&strcasecmp(getenv('REMOTE_ADDR'), 'unknown'))
- {
- $ip = getenv('REMOTE_ADDR');
- }
- elseif(isset($_SERVER['REMOTE_ADDR'])&&$_SERVER['REMOTE_ADDR']&&strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown'))
- {
- $ip = $_SERVER['REMOTE_ADDR'];
- }
- return preg_match("/[\\d\\.]{7,15}/", $ip, $matches) ? $matches[0] : 'unknown';
- }
- //curl封装
- function curl($url, $method = 'GET', $postFields = null, $header = null)
- {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
- curl_setopt($ch, CURLOPT_FAILONERROR, false);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
- curl_setopt($ch, CURLOPT_TIMEOUT, 5);
- if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https")
- {
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- }
- switch ($method)
- {
- case 'POST':
- curl_setopt($ch, CURLOPT_POST, true);
- if (!empty($postFields))
- {
- if (is_array($postFields) || is_object($postFields))
- {
- if (is_object($postFields))
- $postFields = Bin_Tools::object2array($postFields);
- $postBodyString = "";
- $postMultipart = false;
- foreach ($postFields as $k => $v)
- {
- if ("@" != substr($v, 0, 1))
- { //判断是不是文件上传
- $postBodyString .= "$k=" . urlencode($v) . "&";
- }
- else
- { //文件上传用multipart/form-data,否则用www-form-urlencoded
- $postMultipart = true;
- }
- }
- unset($k, $v);
- if ($postMultipart)
- {
- curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
- }
- else
- {
- curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString, 0, -1));
- }
- }
- else
- {
- curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
- }
- }
- break;
- default:
- if (!empty($postFields) && is_array($postFields))
- $url .= (strpos($url, '?') === false ? '?' : '&') . http_build_query($postFields);
- break;
- }
- curl_setopt($ch, CURLOPT_URL, $url);
- if (!empty($header) && is_array($header))
- {
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- }
- $response = curl_exec($ch);
- if (curl_errno($ch))
- {
- throw new Exception(curl_error($ch), 0);
- }
- curl_close($ch);
- return $response;
- }
- //对象转数组
- function object_to_array($obj)
- {
- $_arr = is_object($obj) ? get_object_vars($obj) : $obj;
- foreach ($_arr as $key => $val)
- {
- $val = (is_array($val) || is_object($val)) ? object_to_array($val) : $val;
- $arr[$key] = $val;
- }
- return $arr;
- }
- //发送邮件 基于phpmailer
- function sendmail($to, $title, $content)
- {
- $mail = new lib_phpmail_phpmail();
- $mail->CharSet ="UTF-8";
- $mail->IsSMTP();
- $mail->SMTPAuth = true;
- $mail->SMTPSecure = "ssl";
- $mail->Host = "11.111.11.11:465";
- $mail->Port = 25;
- $mail->Username = "aaa@aaa.com";
- $mail->Password = "123abc";
- $mail->SetFrom('aaa@aaa.com', 'info');
- $mail->AddAddress($to);
- $mail->Ishtml(true);
- $mail->Subject = $title;
- $mail->Body = $content;
- return $mail->Send();
- }
- //加密字符串
- function encrypt($encrypt,$key="key") {
- $iv = mcrypt_create_iv ( mcrypt_get_iv_size ( MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB ), MCRYPT_RAND );
- $passcrypt = mcrypt_encrypt ( MCRYPT_RIJNDAEL_256, $key, $encrypt, MCRYPT_MODE_ECB, $iv );
- $encode = base64_encode ( $passcrypt );
- return $encode;
- }
- //解密字符串
- function decrypt($decrypt,$key="key") {
- $decoded = base64_decode ( $decrypt );
- $iv = mcrypt_create_iv ( mcrypt_get_iv_size ( MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB ), MCRYPT_RAND );
- $decrypted = mcrypt_decrypt ( MCRYPT_RIJNDAEL_256, $key, $decoded, MCRYPT_MODE_ECB, $iv );
- return $decrypted;
- }
- //该片段来自于http://www.codesnippet.cn/detail/1808201410218.html
来源: http://www.codesnippet.cn/detail/1808201410218.html