estamp; if ($seconds > 31536000) { return (int)($seconds / 31536000).'年前'; } elseif ($seconds > 2592000) { return (int)($seconds / 2592000).'个月前'; } elseif ($seconds > 86400) { return (int)($seconds / 86400) . '天前'; } elseif ($seconds > 3600) { return (int)($seconds / 3600) . '小时前'; } elseif ($seconds > 60) { return (int)($seconds / 60) . '分钟前'; } else { return '1分钟前'; } } function humandateLength($timestamp) { $seconds = time() - $timestamp; if ($seconds > 31536000) { return (int)($seconds / 31536000).'年'; } elseif ($seconds > 2592000) { return (int)($seconds / 2592000).'个月'; } elseif ($seconds > 86400) { return (int)($seconds / 86400) . '天'; } return '1天'; } function request_by_fsockopen($url, $host = '') { if (!$host) $host = $_SERVER['SERVER_NAME']; $errno = 0; $errstr = ''; $fp = fsockopen($host, 80, $errno, $errstr, 30); stream_set_blocking($fp, 0); if (!$fp) { $model = D('crontab_log'); $data['result'] = 'error'; $data['url'] = $url; $data['created'] = time(); $model->add($data); } else { $out = "GET " . $url . " HTTP/1.1\r\n"; $out .= "Host: " . $host . "\r\n"; $out .= "Connection: Close\r\n\r\n"; try { fwrite($fp, $out); } catch (Exception $e) { $model = D('crontab_log'); $data['result'] = '错误:'.$e->getMessage(); $data['url'] = $host.$url; $data['created'] = TIME_NOW; $model->add($data); } usleep(20000); fclose($fp); } } function syncRequestData($url, $params) { $ch = curl_init(); curl_setopt($ch,CURLOPT_URL, $url); curl_setopt($ch,CURLOPT_POST,true); curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); curl_setopt($ch,CURLOPT_POSTFIELDS, $params); $content = curl_exec($ch); curl_close($ch); return $content; } function syncRequestJsonData($url, $params) { $content = syncRequestData($url, $params); $retMap = json_decode($content,true); return $retMap; } function aSyncRequestData($url, $params) { $ch = curl_init(); curl_setopt($ch,CURLOPT_URL, $url); curl_setopt($ch,CURLOPT_POST,true); curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); curl_setopt($ch, CURLOPT_TIMEOUT, 0); curl_setopt($ch,CURLOPT_POSTFIELDS, $params); $content = curl_exec($ch); curl_close($ch); return $content; } function post_file($url, $params, $filePath) { $params["file"] = new \CURLFile(realpath($filePath)); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $params); $content = curl_exec($ch); if($content === false) { $errMsg = 'Curl error: ' . curl_error($ch); $ret = array(); $ret['data'] = array(); $ret['err_code'] = 999; $ret['err_msg'] = $errMsg; return $ret; } curl_close($ch); return json_decode($content,true);; } function request_by_fsockopen_post($url,$host='',$data=array()) { $post = http_build_query($data); $len = strlen($post); if(!$host) $host = $_SERVER['SERVER_NAME']; $errno = 0; $errstr = ''; $fp = fsockopen( $host , 80, $errno, $errstr, 30); if (!$fp) { $model = D('crontab_log'); $data['result'] = "$errstr ($errno)\n"; $data['url'] = $host.$url; $data['created'] = TIME_NOW; $model->add($data); } else { $out = "POST $url HTTP/1.1\r\n"; $out .= "Host: $host\r\n"; $out .= "Content-type: application/x-www-form-urlencoded\r\n"; $out .= "Connection: Close\r\n"; $out .= "Content-Length: $len\r\n"; $out .= "\r\n"; $out .= $post."\r\n"; try { fwrite($fp, $out); } catch (Exception $e) { $model = D('crontab_log'); $data['result'] = '错误:'.$e->getMessage(); $data['url'] = $host.$url; $data['created'] = TIME_NOW; $model->add($data); } fclose($fp); } } function saveFileToGZip($fileName, $content) { set_time_limit(0); $gzfile = $fileName . ".gz"; $fp = gzopen($gzfile, 'w9'); gzwrite($fp, $content); gzclose($fp); unset($content); if (file_exists($gzfile)) { return true; } else { return false; } } function saveFileToGZipByAppend($fileName, $content) { set_time_limit(0); $gzfile = $fileName . ".gz"; try { if (file_exists($gzfile)) { $lines = gzfile($gzfile); $lines[count($lines)] = "$content "; $input = implode($lines); } else { $input = "$c