common.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /*
  3. * curl 调用
  4. */
  5. //echo post_request("http://www.baidu.com");
  6. function post_request($url, $params=array()) {
  7. $ch = curl_init();
  8. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  9. //curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded"));
  10. curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/html"));
  11. curl_setopt($ch, CURLOPT_URL, $url);
  12. curl_setopt($ch, CURLOPT_POST, true);
  13. curl_setopt($ch, CURLOPT_TIMEOUT, 3);
  14. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  15. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
  16. $result = curl_exec($ch);
  17. $httpinfo= curl_getinfo($ch);
  18. curl_close($ch);
  19. //print_r($httpinfo);
  20. $info = "{$httpinfo['http_code']}|$url|$result";
  21. Log::save_run_log($info,'curl');
  22. if($httpinfo['http_code'] == 200){
  23. return $result;
  24. } else {
  25. return false;
  26. }
  27. }
  28. function xml_unserialize(&$xml, $isnormal = FALSE) {
  29. $xml_parser = new XML($isnormal);
  30. $data = $xml_parser->parse($xml);
  31. $xml_parser->destruct();
  32. return $data;
  33. }
  34. /**
  35. * 获得请求端的xml
  36. */
  37. function post_request_xml($url, $params=array()) {
  38. $ch = curl_init();
  39. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  40. //curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded"));
  41. curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/html"));
  42. curl_setopt($ch, CURLOPT_URL, $url);
  43. curl_setopt($ch, CURLOPT_POST, true);
  44. curl_setopt($ch, CURLOPT_TIMEOUT, 3);
  45. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  46. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
  47. $result = file_get_contents('php://input');
  48. $httpinfo= curl_getinfo($ch);
  49. curl_close($ch);
  50. //print_r($httpinfo);
  51. $info = "{$httpinfo['http_code']}|$url|$result";
  52. Log::save_run_log($info,'curl');
  53. if($httpinfo['http_code'] == 200){
  54. return $result;
  55. } else {
  56. return false;
  57. }
  58. }
  59. /**
  60. * 获得请求端的ip地址
  61. */
  62. function get_client_ip() {
  63. global $_SERVER;
  64. if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
  65. $ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
  66. } elseif (isset($_SERVER["HTTP_CLIENT_IP"])) {
  67. $ip = $_SERVER["HTTP_CLIENT_IP"];
  68. } else {
  69. $ip = $_SERVER["REMOTE_ADDR"];
  70. }
  71. return $ip;
  72. }
  73. /**
  74. * 接口999操作失败
  75. * forexample: response_999(__FILE__,__LINE__,__FUNCTION__,"SQL");
  76. */
  77. function response_999($file,$line,$function,$err_type){
  78. $parameters = get_parameters();
  79. $file = strstr($file,"u.");
  80. $log = "|response:999|file:$file|line:$line|function:$function|err_type:$err_type|$parameters";
  81. Log::save_run_log($log,"run");
  82. echo "-999";
  83. exit;
  84. }
  85. /**
  86. * 接口返回结果函数
  87. */
  88. function response($result){
  89. echo $result;
  90. exit;
  91. }
  92. /**
  93. * 取得精确时间值
  94. */
  95. function microtime_float(){
  96. list($usec, $sec) = explode(" ", microtime());
  97. return ((float)$usec + (float)$sec);
  98. }
  99. /**
  100. * 取得请求参数,并组成字符串
  101. * 格式为 {parameter:value|parameter:value|parameter:value|parameter:value……………………}
  102. */
  103. function get_parameters() {
  104. global $_GET,$_POST;
  105. $parameters = "";
  106. if($_GET){
  107. foreach($_GET as $key => $val){
  108. if($key == 'sign')
  109. continue;
  110. else
  111. $parameters .= "$key:$val|";
  112. }
  113. }
  114. if($_POST){
  115. foreach($_POST as $key => $val){
  116. if($key == 'sign')
  117. continue;
  118. else
  119. $parameters .= "$key:$val|";
  120. }
  121. }
  122. if(strlen($parameters) > 1 )
  123. $parameters = substr($parameters,0,-1);
  124. return "{".$parameters."}";
  125. }
  126. function get_browser_language() {
  127. if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])){
  128. $accept_lang = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
  129. return $accept_lang[0];
  130. } else {
  131. return false;
  132. }
  133. }
  134. function filterInput(&$info){
  135. if(empty($info)) return false;
  136. if(is_array($info)){
  137. foreach ($info as $key=>$val){
  138. $info[$key] = addslashes($val);
  139. }
  140. } else {
  141. $info = addslashes($info);
  142. }
  143. }
  144. function matchSex($total_friend,$sex,$friends){
  145. $ran = rand(0,$total_friend-1);
  146. if($sex == 2) return $ran;
  147. if ($sex == 1 and $friends[$ran]['sex']=='male'){
  148. return $ran;
  149. } else if ($sex == 0 and $friends[$ran]['sex']=='female'){
  150. return $ran;
  151. } else {
  152. $ran = matchSex($total_friend,$sex,$friends);
  153. }
  154. }
  155. function get_GetParameters() {
  156. global $_GET;
  157. $parameters = "";
  158. if($_GET){
  159. foreach($_GET as $key => $val){
  160. $parameters .= "$key:$val|";
  161. }
  162. }
  163. if(strlen($parameters) > 1 )
  164. $parameters = substr($parameters,0,-1);
  165. return $parameters;
  166. }
  167. function make_curl($url,$params=array()){
  168. $ch = curl_init();
  169. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  170. curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded"));
  171. curl_setopt($ch, CURLOPT_URL, $url);
  172. curl_setopt($ch, CURLOPT_POST, true);
  173. curl_setopt($ch, CURLOPT_TIMEOUT, 60);
  174. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  175. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  176. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
  177. $result = curl_exec($ch);
  178. $result = json_decode($result,true);
  179. return $result;
  180. }
  181. ?>