PageRenderTime 29ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/apps/Common/Library/Sms/Curl.class.php

https://gitlab.com/fangjianwei/weifenxiao
PHP | 186 lines | 119 code | 17 blank | 50 comment | 9 complexity | d703a125b1a474125a04566f5a589ae7 MD5 | raw file
  1. <?php
  2. /**
  3. * 重写Curl类
  4. *
  5. * @package Curl
  6. * @author weitao
  7. */
  8. namespace Common\Library\Sms;
  9. class Curl{
  10. static $headers = array('Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg','Connection: Keep-Alive','Content-type: application/x-www-form-urlencoded;charset=UTF-8');
  11. static $user_agent = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0)';
  12. static $compression = 'gzip';
  13. static $cookie_file;
  14. static $proxy = '';
  15. static $key = KEYSMSAPI;
  16. /**
  17. * 模拟GET的方法
  18. * @param $url
  19. * @return mixed
  20. */
  21. static function get($url){
  22. $ch = curl_init($url);
  23. curl_setopt($ch, CURLOPT_HTTPHEADER, self::$headers);
  24. curl_setopt($ch, CURLOPT_HEADER, 0);
  25. curl_setopt($ch, CURLOPT_USERAGENT, self::$user_agent);
  26. curl_setopt($ch, CURLOPT_ENCODING, self::$compression);
  27. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  28. if(self::$proxy) curl_setopt($ch, CURLOPT_PROXY, self::$proxy);
  29. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  30. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  31. $result = curl_exec($ch);
  32. curl_close($ch);
  33. return $result;
  34. }
  35. /**
  36. * 模拟POST的方法
  37. * @param $url
  38. * @param $data
  39. * @return mixed
  40. *
  41. */
  42. static function post($url,$data){
  43. $ch = curl_init($url);
  44. curl_setopt($ch, CURLOPT_HTTPHEADER, self::$headers);
  45. curl_setopt($ch, CURLOPT_HEADER, 1);
  46. curl_setopt($ch, CURLOPT_USERAGENT, self::$user_agent);
  47. curl_setopt($ch, CURLOPT_ENCODING, self::$compression);
  48. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  49. if(self::$proxy) curl_setopt($ch,CURLOPT_PROXY, self::$proxy);
  50. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
  51. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  52. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  53. curl_setopt($ch, CURLOPT_POST, 1);
  54. $result = curl_exec($ch);
  55. curl_close($ch);
  56. return $result;
  57. }
  58. /**
  59. * 远程请求方法
  60. * @author Diven(702814242@qq.com)
  61. * @date 2014-07-12 15:47pm
  62. */
  63. static function request($url, $params, $cookie = '', $method = 'POST')
  64. {
  65. $queryString = http_build_query($params);
  66. $cookieString = self::getCookieString($cookie);
  67. $ch = curl_init();
  68. if (strtoupper($method) == 'GET') {
  69. $curl = strpos($url, '?') > 0 ? $url.'&'.$queryString : $url.'?'.$queryString;
  70. curl_setopt($ch, CURLOPT_URL, $curl);
  71. }else{
  72. curl_setopt($ch, CURLOPT_URL, $url);
  73. curl_setopt($ch, CURLOPT_POST, 1);
  74. curl_setopt($ch, CURLOPT_POSTFIELDS, $queryString);
  75. }
  76. curl_setopt($ch, CURLOPT_HEADER, false);
  77. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  78. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
  79. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
  80. if (!empty($cookieString))
  81. {
  82. curl_setopt($ch, CURLOPT_COOKIE, $cookieString);
  83. }
  84. $res = curl_exec($ch);
  85. curl_close($ch);
  86. return $res;
  87. }
  88. static private function getCookieString($params)
  89. {
  90. if (is_string($params)){
  91. return $params;
  92. }
  93. $str = '';
  94. foreach ($params as $k => $v)
  95. {
  96. $str .= $k.'='.$v.';' ;
  97. }
  98. return rtrim($str,';');
  99. }
  100. /**
  101. * url加密方法
  102. * @author xiaodong
  103. * @param $data 加密数据
  104. * @return 加密字符串
  105. *
  106. */
  107. static function encode($data){
  108. return base64_encode(CookieCrypt::encrypt(json_encode($data), self::$key));
  109. }
  110. /**
  111. * url解密方法
  112. * @author xiaodong
  113. * @param $value 解密字符串
  114. * @return 解密数据
  115. *
  116. */
  117. static function decode($value){
  118. $v = json_decode(CookieCrypt::decrypt(base64_decode($value), self::$key));
  119. return is_scalar($v) ? $v : (array) $v;
  120. }
  121. /*
  122. * Api统一请求出口,用verify统一传递加密字符串 ,在api端获取verify参数然后再解密
  123. * @author Diven
  124. */
  125. static function requestApi($url, $data, $cookie = '', $type = 'POST')
  126. {
  127. $param = array('verify'=>self::encode($data));
  128. return Curl::request($url,$param,$cookie,$type);
  129. }
  130. static function getApiResponse($url, $data, $cookie = '', $type = 'POST', $force=true)
  131. {
  132. $ret=self::requestApi($url, $data, $cookie, $type);
  133. return self::decodeJson($ret,$force);
  134. }
  135. /*
  136. * json返回解密的数据
  137. * 默认$force=true时,以数组格式返回,否则以对象返回
  138. * @author Ocean
  139. */
  140. static function decodeJson($value,$force=true)
  141. {
  142. $decrypt=CookieCrypt::decrypt(base64_decode($value), self::$key);
  143. if(is_null($decrypt))
  144. return $value;
  145. if($force)
  146. {
  147. $v = json_decode($decrypt);
  148. return self::object_to_array($v);
  149. }else{
  150. $v = json_decode($decrypt);
  151. return $v;
  152. }
  153. }
  154. /*
  155. * 递归将json对象转化为数组
  156. * @author Ocean
  157. */
  158. static function object_to_array($obj)
  159. {
  160. $arr=array();
  161. $_arr = is_object($obj) ? get_object_vars($obj) : $obj;
  162. foreach ($_arr as $key => $val)
  163. {
  164. $val = (is_array($val) || is_object($val)) ? self::object_to_array($val) : $val;
  165. $arr[$key] = $val;
  166. }
  167. return $arr;
  168. }
  169. }