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

https://gitlab.com/fangjianwei/weifenxiao · PHP · 182 lines · 119 code · 16 blank · 47 comment · 9 complexity · 12b195f5914d9707f55418474a2e3676 MD5 · raw file

  1. <?php
  2. namespace Common\Library\Curl;
  3. use Common\Library\Curl\crypt;
  4. /**
  5. * 重写Curl类
  6. *
  7. * @package Curl
  8. * @author weitao
  9. */
  10. class Curl{
  11. 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');
  12. 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)';
  13. static $compression = 'gzip';
  14. static $proxy = '';
  15. static $key = URLKEY;
  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, 0);
  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, 3);
  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. * @param $data 加密数据
  103. * @return 加密字符串
  104. *
  105. */
  106. static function encode($data){
  107. return base64_encode(Crypt::encrypt(json_encode($data), self::$key));
  108. }
  109. /**
  110. * url解密方法
  111. * @param $value 解密字符串
  112. * @return 解密数据
  113. *
  114. */
  115. static function decode($value){
  116. $v = json_decode(Crypt::decrypt(base64_decode($value), self::$key));
  117. return is_scalar($v) ? $v : (array) $v;
  118. }
  119. /*
  120. * Api统一请求出口,用verify统一传递加密字符串 ,在api端获取verify参数然后再解密
  121. * @author Diven
  122. */
  123. static function requestApi($url, $data, $cookie = '', $type = 'POST')
  124. {
  125. $param = array('verify'=>self::encode($data));
  126. return Curl::request($url,$param,$cookie,$type);
  127. }
  128. /*
  129. * json返回解密的数据
  130. * 默认$force=true时,以数组格式返回,否则以对象返回
  131. * @author Ocean
  132. */
  133. static function decodeJson($value,$force=true)
  134. {
  135. $decrypt=Crypt::decrypt(base64_decode($value), self::$key);
  136. if(is_null($decrypt))
  137. return $value;
  138. if($force)
  139. {
  140. $v = json_decode($decrypt);
  141. return self::object_to_array($v);
  142. }else{
  143. $v = json_decode($decrypt);
  144. return $v;
  145. }
  146. }
  147. /*获得发送请求后的返回数据
  148. *
  149. */
  150. static function getApiResponse($url, $data, $cookie = '', $type = 'POST', $force=true)
  151. {
  152. $ret=self::requestApi($url, $data, $cookie, $type);
  153. return self::decodeJson($ret,$force);
  154. }
  155. static function object_to_array($obj)
  156. {
  157. $arr=array();
  158. $_arr = is_object($obj) ? get_object_vars($obj) : $obj;
  159. foreach ($_arr as $key => $val)
  160. {
  161. $val = (is_array($val) || is_object($val)) ? self::object_to_array($val) : $val;
  162. $arr[$key] = $val;
  163. }
  164. return $arr;
  165. }
  166. }