functionGetIP () {if(!Empty($_server["Http_client_ip"])){ $cip=$_server["Http_client_ip"]; }ElseIf(!Empty($_server["Http_x_forwarded_for"])){ $cip=$_server["Http_x_forwarded_for"]; }ElseIf(!Empty($_server["REMOTE_ADDR"])){ $cip=$_server["REMOTE_ADDR"]; }Else{ $cip= ""; } return $cip;}
functionHttpRequest ($url,$type= ' Get ',$data= ",$timeout= 10) { $header=Array(); if($type= = ' Head ') { $header[] = "content-type:text/xml; Charset=utf-8 "; }Else if($type= = ' Get '){ $header[] = "Accept: */*"; $header[] = "ACCEPT-ENCODING:GZIP,DEFLATE,SDCH"; $header[] = "Connection:keep-alive"; } $ch=Curl_init (); curl_setopt ($ch, Curlopt_url,$url); curl_setopt ($ch, Curlopt_httpheader,$header); if($type! = ' Get ' &&!Empty($data) {curl_setopt ($ch, Curlopt_post, 1); curl_setopt ($ch, Curlopt_postfields,$data); } curl_setopt ($ch, Curlopt_returntransfer, 1); curl_setopt ($ch, Curlopt_header, 0); curl_setopt ($ch, Curlopt_timeout,$timeout); curl_setopt ($ch, curlopt_encoding, ' gzip '); $result[' response '] = curl_exec ($ch); $result[' Status ']=curl_getinfo ($ch,Curlinfo_http_code); Curl_close ($ch); return $result; }
functionSavelog ($content= ",$filename= ' Others ') { $rootDir= \config::get (' App.rootdir '); $logdir=$rootDir. '/app/storage/logs/'; if(!Is_dir($logdir))mkdir($logdir, 0777,true); $filename=$filename.‘ _‘.Date(' Ymd '); $filename=$logdir.$filename. ". Log"; $fp=fopen($filename, "A +"); $line= 1; while(Stream_get_line($fp, 8192, "\ n")) { $line++; } if($line> 9999) { file_put_contents($filename, ‘‘); $line= 1; } $info= ' < '.sprintf("%04d",$line) . ' > '.Date("Y-m-d h:i:s"). ' <> '; $string=$info.Str_replace("\ n", "",Str_replace("\ R", "",$content)) . "\ r \ n"; file_put_contents($filename,$string,file_append); fclose($fp); }
/** * Get or save file contents * @param string $filedir file path * @param string $content file contents * @return string*/ functionFilecontent ($filedir= ",$content= ' ') { if(Empty($filedir))return‘‘; if(Empty($content)) { if(file_exists($filedir)) { $fp=fopen($filedir, "R"); $content=file_get_contents($filedir); fclose($fp); return $content; } Else { return‘‘; } } Else { $fps=fopen($filedir, "a"); file_put_contents($filedir,$content); fclose($fps); return true; } }
PHP Personal common function encapsulation