123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394 |
- <?php
- /* Desc : 修改 common.php 为Class版本,方便使用
- *
- * Para :
- *
- * Author:
- *
- **/
- class commonFunc{
- /*
- * curl 调用
- */
- //echo post_request("http://www.baidu.com");
- public static function post_request($url, $params=array()) {
-
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
- curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded"));
- //curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/html"));
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_TIMEOUT, 3);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
- $result = curl_exec($ch);
- $httpinfo= curl_getinfo($ch);
- curl_close($ch);
- //print_r($httpinfo);
- $info = "{$httpinfo['http_code']}|$url|$result";
- Log::save_run_log($info,'curl');
- if($httpinfo['http_code'] == 200){
- return $result;
- } else {
- return false;
- }
- }
-
- public static function makeRequest($url, $params,$timeout=3,$post=true) {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
- curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded"));
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_POST, $post);
- curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
- curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
- $result = curl_exec($ch);
- curl_close($ch);
- return $result;
- }
-
-
- public static function get_request_https($url){
- $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, true);
- curl_setopt($ch, CURLOPT_TIMEOUT, 15);
- if(TEST) curl_setopt($ch, CURLOPT_PROXY, '127.0.0.1:8086');
- $result = curl_exec($ch);
- $httpinfo = curl_getinfo($ch);
-
- $info = "{$httpinfo['http_code']}|$url|$result";
- Log::save_run_log($info,'curlhttps');
-
- curl_close($ch);
- if($httpinfo['http_code'] == 200){
- return $result;
- } else {
- return false;
- }
- }
-
- public static function xml_unserialize(&$xml, $isnormal = FALSE) {
- $xml_parser = new XML($isnormal);
- $data = $xml_parser->parse($xml);
- $xml_parser->destruct();
- return $data;
- }
-
- /**
- * 获得请求端的xml
- */
-
- public static function post_request_xml($url, $params=array()) {
-
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
- //curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded"));
- curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/html"));
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_TIMEOUT, 3);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
-
- $result = file_get_contents('php://input');
-
- $httpinfo= curl_getinfo($ch);
- curl_close($ch);
- //print_r($httpinfo);
- $info = "{$httpinfo['http_code']}|$url|$result";
- Log::save_run_log($info,'curl');
- if($httpinfo['http_code'] == 200){
- return $result;
- } else {
- return false;
- }
- }
- /**
- * 获得请求端的ip地址
- */
- public static function get_client_ip() {
- global $_SERVER;
- if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
- $ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
- } elseif (isset($_SERVER["HTTP_CLIENT_IP"])) {
- $ip = $_SERVER["HTTP_CLIENT_IP"];
- } else {
- $ip = $_SERVER["REMOTE_ADDR"];
- }
- return $ip;
- }
-
- /**
- * 接口999操作失败
- * forexample: response_999(__FILE__,__LINE__,__FUNCTION__,"SQL");
- */
- public static function response_999($file,$line,$function,$err_type){
- $parameters = get_parameters();
- $file = strstr($file,"u.");
- $log = "|response:999|file:$file|line:$line|function:$function|err_type:$err_type|$parameters";
- Log::save_run_log($log,"run");
- echo "-999";
- exit;
- }
- /**
- * 接口返回结果函数
- */
- public static function response($result){
- echo $result;
- exit;
- }
-
- /**
- * 取得精确时间值
- */
- public static function microtime_float(){
- list($usec, $sec) = explode(" ", microtime());
- return ((float)$usec + (float)$sec);
- }
-
-
- /**
- * 取得请求参数,并组成字符串
- * 格式为 {parameter:value|parameter:value|parameter:value|parameter:value……………………}
- */
- public static function get_parameters() {
- global $_GET,$_POST;
- $parameters = "";
- if($_GET){
- foreach($_GET as $key => $val){
- if($key == 'sign')
- continue;
- else
- $parameters .= "$key:$val|";
- }
- }
- if($_POST){
- foreach($_POST as $key => $val){
- if($key == 'sign')
- continue;
- else
- $parameters .= "$key:$val|";
- }
- }
- if(strlen($parameters) > 1 )
- $parameters = substr($parameters,0,-1);
- return "{".$parameters."}";
- }
-
- public static function get_browser_language() {
- if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])){
- $accept_lang = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
- return $accept_lang[0];
- } else {
- return false;
- }
- }
-
-
- public static function filterInput(&$info){
- if(empty($info)) return false;
- if(is_array($info)){
- foreach ($info as $key=>$val){
- $info[$key] = addslashes($val);
- }
- } else {
- $info = addslashes($info);
- }
- }
-
- public static function matchSex($total_friend,$sex,$friends){
- $ran = rand(0,$total_friend-1);
-
- if($sex == 2) return $ran;
- if ($sex == 1 and $friends[$ran]['sex']=='male'){
- return $ran;
- } else if ($sex == 0 and $friends[$ran]['sex']=='female'){
- return $ran;
- } else {
- $ran = matchSex($total_friend,$sex,$friends);
- }
- }
-
- public static function get_GetParameters() {
- global $_GET;
- $parameters = "";
- if($_GET){
- foreach($_GET as $key => $val){
- $parameters .= "$key:$val|";
- }
- }
- if(strlen($parameters) > 1 )
- $parameters = substr($parameters,0,-1);
- return $parameters;
- }
-
- public static function getRandomStr( $length = 8 )
- {
- // character dictionary
- $chars = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
- 'i', 'j', 'k', 'l','m', 'n', 'o', 'p', 'q', 'r', 's',
- 't', 'u', 'v', 'w', 'x', 'y','z', 'A', 'B', 'C', 'D',
- 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L','M', 'N', 'O',
- 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y','Z',
- '0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
-
- // get keys
- $max = count($chars)-1;
- for ($i = 0; $i < $length; $i++){
- $keys[] = mt_rand(0,$max);
- }
-
- $password = '';
- for($i = 0; $i < $length; $i++)
- {
- $password .= $chars[$keys[$i]];
- }
-
- return $password;
- }
-
- public static function getRandomNum( $length = 6 )
- {
- // character dictionary
- $chars = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
-
- // get keys
- $max = count($chars)-1;
- for ($i = 0; $i < $length; $i++){
- $keys[] = mt_rand(0,$max);
- }
-
- $password = '';
- for($i = 0; $i < $length; $i++)
- {
- $password .= $chars[$keys[$i]];
- }
-
- return $password;
- }
-
- public static function getUrlHost($url){
-
- $host_end_pos = strpos($url,'/',7);
- if(!empty($host_end_pos)){
- $hostUrl = substr($url,0,$host_end_pos);
- }
- else{
- $hostUrl = $url;
- }
- return $hostUrl;
- }
-
- public static function handlePageRefer($logFile_pre){
- // Save refer log
- $refer = $_SERVER['HTTP_REFERER'];
- if(!empty($refer) && strpos($refer,'oasgames.com') === FALSE){
- $refer = commonFunc::getUrlHost($refer);
- Log::save_run_log($refer,$logFile_pre);
- // set cookie
- setcookie("oas_lp_refer",$refer,0,'/','.oasgames.com');
- }
- }
-
- public static function sqlInjectCheck($input){
-
- // $sqlKeyWords = array(
- // 'select','insert','update',
- // 'delete','drop','create',
- // 'truncate','database',
- // 'table','column','add',
- // 'load_file','grant','privileges',
- // ' ',
- // );
- //
- // $input = strtolower($input);
- //
- // foreach ($sqlKeyWords as $kw){
- // if(strpos($input, $kw) !== FALSE){
- // return true;
- // }
- // }
- return false;
- }
-
- // Desc: Method to make request at server side
- // Para:
- // @$default_ip can be set with the ip of the url host
- //
- // Implemented by Huchunmei at 2013.2.27
- public static function curlMakeRequest($url, $params,$timeout=3,$isPostMethod=true,$default_ip='') {
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
- curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded"));
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_HTTPPROXYTUNNEL, 0);
- curl_setopt($curl, CURLOPT_POST, $isPostMethod);
- curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($params));
- curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
-
- if(!empty($default_ip))
- curl_setopt($curl, CURLOPT_PROXY,$default_ip);
-
- $output = curl_exec($curl);
- $httpinfo = curl_getinfo($curl);
- curl_close($curl);
- if($httpinfo['http_code'] == 200){
- return $output;
- } else {
- return false;
- }
- }
- public static function curlRequest($url,$postData='',$timeout=3){
-
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);
- curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
- curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
-
- if(!empty($postData)){
- curl_setopt($curl, CURLOPT_POST, 1);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
- }
- curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
-
- $resultData = curl_exec($curl);
- $httpInfo = curl_getinfo($curl);
-
- if (curl_errno($curl)) {
- curl_close($curl);
- return false;
- }else{
- curl_close($curl);
- return $resultData;
- }
-
- }
-
- // Class end
- }
- ?>
|