NetUtils.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. // require_once 'includes/WebStart.php';
  3. class NetUtils {
  4. /**
  5. * Set up the API root URL.
  6. *
  7. * @ignore
  8. *
  9. *
  10. *
  11. *
  12. */
  13. public $host = "https://api.vdian.com/";
  14. /**
  15. * Set timeout default.
  16. *
  17. * @ignore
  18. *
  19. *
  20. *
  21. *
  22. */
  23. public $timeout = 30;
  24. /**
  25. * Set connect timeout.
  26. *
  27. * @ignore
  28. */
  29. public $connecttimeout = 30;
  30. /**
  31. * Verify SSL Cert.
  32. *
  33. * @ignore
  34. */
  35. public $ssl_verifypeer = FALSE;
  36. /**
  37. * Respons format.
  38. * @ignore
  39. */
  40. public $format = 'json';
  41. /**
  42. * Decode returned json data.
  43. * @ignore
  44. */
  45. public $decode_json = TRUE;
  46. /**
  47. * Contains the last HTTP headers returned.
  48. * @ignore
  49. */
  50. public $http_info;
  51. /**
  52. * Set the useragnet.
  53. * @ignore
  54. */
  55. private $useragent = 'weidian OAuth2 PHP SDK v1.0';
  56. /**
  57. * boundary of multipart
  58. * @ignore
  59. */
  60. public static $boundary = '';
  61. public static function getInstance() {
  62. return new NetUtils ();
  63. }
  64. /**
  65. * HTTP GET 请求
  66. *
  67. * @param unknown $url
  68. * @return HttpResponse
  69. */
  70. public function get($url) {
  71. if (strrpos ( $url, 'http://' ) !== 0 && strrpos ( $url, 'https://' ) !== 0) {
  72. $url = "{$this->host}{$url}.{$this->format}";
  73. }
  74. return $this->http ( $url, 'GET' );
  75. }
  76. /**
  77. * POST request
  78. *
  79. * @param string $url
  80. * @param array $parameters
  81. * @return HttpResponse
  82. */
  83. public function post($url, array $parameters) {
  84. if (strrpos ( $url, 'http://' ) !== 0 && strrpos ( $url, 'https://' ) !== 0) {
  85. $url = "{$this->host}{$url}.{$this->format}";
  86. }
  87. $body = null;
  88. if (DEBUG) {
  89. Logger::debug ( "URL->" . $url );
  90. if (is_array ( $parameters )) {
  91. while ( list ( $key, $val ) = each ( $parameters ) ) {
  92. Logger::debug ( $key . "=" . $val );
  93. }
  94. }
  95. }
  96. $body = http_build_query ( $parameters );
  97. return $this->http ( $url, 'POST', $body );
  98. }
  99. /**
  100. * 发送HTTP请求
  101. *
  102. * @param string $url
  103. * @param string $method
  104. * @param unknown $parameters
  105. * @param bool $multi
  106. * @return HttpResponse
  107. */
  108. public function request($url, $method, $parameters, $multi = false) {
  109. if (strrpos ( $url, 'http://' ) !== 0 && strrpos ( $url, 'https://' ) !== 0) {
  110. // 支持相对路径
  111. $url = "{$this->host}{$url}.{$this->format}";
  112. }
  113. switch ($method) {
  114. case 'GET' :
  115. $url = $url . '?' . http_build_query ( $parameters );
  116. return $this->http ( $url, 'GET' );
  117. default :
  118. $headers = array ();
  119. if (! $multi && (is_array ( $parameters ) || is_object ( $parameters ))) {
  120. $body = http_build_query ( $parameters );
  121. } else {
  122. $body = self::build_http_query_multi ( $parameters );
  123. $headers [] = "Content-Type: multipart/form-data; boundary=" . self::$boundary;
  124. }
  125. return $this->http ( $url, $method, $body, $headers );
  126. }
  127. }
  128. /**
  129. * 上传文件
  130. *
  131. * @param unknown $url
  132. * @param unknown $parameters
  133. * @return Ambigous <string, mixed>
  134. */
  135. public function uploadFile($url, $parameters) {
  136. $headers = array ();
  137. $body = self::build_http_query_multi ( $parameters );
  138. $headers [] = "Content-Type: multipart/form-data; boundary=" . self::$boundary;
  139. return self::http ( $url, 'POST', $body, $headers );
  140. }
  141. /**
  142. * Make an HTTP request
  143. *
  144. * @return string API results
  145. * @ignore
  146. *
  147. *
  148. *
  149. *
  150. */
  151. private function http($url, $method, $postfields = NULL, $headers = array()) {
  152. $response = new HttpResponse ( $url, $headers, $method, $postfields );
  153. $ci = curl_init ();
  154. /* Curl settings */
  155. curl_setopt ( $ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0 );
  156. curl_setopt ( $ci, CURLOPT_USERAGENT, $this->useragent );
  157. curl_setopt ( $ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout );
  158. curl_setopt ( $ci, CURLOPT_TIMEOUT, $this->timeout );
  159. curl_setopt ( $ci, CURLOPT_RETURNTRANSFER, TRUE );
  160. curl_setopt ( $ci, CURLOPT_ENCODING, "UTF-8" );
  161. curl_setopt ( $ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer );
  162. curl_setopt ( $ci, CURLOPT_SSL_VERIFYHOST, "2" );
  163. curl_setopt ( $ci, CURLOPT_HEADERFUNCTION, array (
  164. $this,
  165. 'getHeader'
  166. ) );
  167. curl_setopt ( $ci, CURLOPT_HEADER, FALSE );
  168. switch ($method) {
  169. case 'POST' :
  170. curl_setopt ( $ci, CURLOPT_POST, TRUE );
  171. if (! empty ( $postfields )) {
  172. curl_setopt ( $ci, CURLOPT_POSTFIELDS, $postfields );
  173. $this->postdata = $postfields;
  174. }
  175. break;
  176. case 'DELETE' :
  177. curl_setopt ( $ci, CURLOPT_CUSTOMREQUEST, 'DELETE' );
  178. if (! empty ( $postfields )) {
  179. $url = "{$url}?{$postfields}";
  180. }
  181. }
  182. $headers [] = "API-RemoteIP: " . $_SERVER ['REMOTE_ADDR'];
  183. curl_setopt ( $ci, CURLOPT_URL, $url );
  184. curl_setopt ( $ci, CURLOPT_HTTPHEADER, $headers );
  185. curl_setopt ( $ci, CURLINFO_HEADER_OUT, TRUE );
  186. $response->data = curl_exec ( $ci );
  187. $response->http_code = curl_getinfo ( $ci, CURLINFO_HTTP_CODE );
  188. $response->http_info = array_merge ( $response->http_info, curl_getinfo ( $ci ) );
  189. $response->url = $url;
  190. if (DEBUG) {
  191. Logger::debug ( "headers:" );
  192. print_r ( $headers );
  193. Logger::debug ( "requestInfo:" );
  194. print_r ( curl_getinfo ( $ci ) );
  195. Logger::debug ( "body:" );
  196. print_r ( $postfields );
  197. }
  198. Logger::debug ( "返回:" . $response->data );
  199. curl_close ( $ci );
  200. return $response;
  201. }
  202. /**
  203. *
  204. * @ignore
  205. *
  206. *
  207. *
  208. */
  209. private function build_http_query_multi($params) {
  210. if (! $params)
  211. return '';
  212. uksort ( $params, 'strcmp' );
  213. $pairs = array ();
  214. self::$boundary = $boundary = uniqid ( '===============' );
  215. $MPboundary = '--' . $boundary;
  216. $endMPboundary = $MPboundary . '--';
  217. $multipartbody = '';
  218. foreach ( $params as $parameter => $value ) {
  219. if (in_array ( $parameter, array (
  220. 'media'
  221. ) ) && $value {0} == '@') {
  222. $url = ltrim ( $value, '@' );
  223. $content = file_get_contents ( $url );
  224. $array = explode ( '?', basename ( $url ) );
  225. $filename = $array [0];
  226. $multipartbody .= $MPboundary . "\r\n";
  227. $multipartbody .= 'Content-Disposition: form-data; name="' . $parameter . '"; filename="' . $filename . '"' . "\r\n";
  228. $multipartbody .= "Content-Type: image/unknown\r\n\r\n";
  229. $multipartbody .= $content . "\r\n";
  230. } else {
  231. $multipartbody .= $MPboundary . "\r\n";
  232. $multipartbody .= 'content-disposition: form-data; name="' . $parameter . "\"\r\n\r\n";
  233. $multipartbody .= $value . "\r\n";
  234. }
  235. }
  236. $multipartbody .= $endMPboundary;
  237. return $multipartbody;
  238. }
  239. /**
  240. * Get the header info to store.
  241. *
  242. * @return int
  243. * @ignore
  244. *
  245. *
  246. *
  247. *
  248. *
  249. *
  250. */
  251. function getHeader($ch, $header) {
  252. $i = strpos ( $header, ':' );
  253. if (! empty ( $i )) {
  254. $key = str_replace ( '-', '_', strtolower ( substr ( $header, 0, $i ) ) );
  255. $value = trim ( substr ( $header, $i + 2 ) );
  256. $this->http_header [$key] = $value;
  257. }
  258. return strlen ( $header );
  259. }
  260. }
  261. ;
  262. ?>