SnsNetwork.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * 发送HTTP网络请求类
  4. *
  5. * @version 3.0.0
  6. * @author open.qq.com
  7. * @copyright © 2012, Tencent Corporation. All rights reserved.
  8. * @ History:
  9. * 3.0.1 | coolinchen | 2012-09-07 10:30:00 | add funtion makeRequestWithFile
  10. * 3.0.0 | nemozhang | 2011-03-09 15:33:04 | initialization
  11. */
  12. class SnsNetwork
  13. {
  14. /**
  15. * 执行一个 HTTP 请求
  16. *
  17. * @param string $url 执行请求的URL
  18. * @param mixed $params 表单参数
  19. * 可以是array, 也可以是经过url编码之后的string
  20. * @param mixed $cookie cookie参数
  21. * 可以是array, 也可以是经过拼接的string
  22. * @param string $method 请求方法 post / get
  23. * @param string $protocol http协议类型 http / https
  24. * @return array 结果数组
  25. */
  26. public static function makeRequest($url, $params, $cookie, $method='post', $protocol='http')
  27. {
  28. $query_string = self::makeQueryString($params);
  29. $cookie_string = self::makeCookieString($cookie);
  30. $ch = curl_init();
  31. if ('GET' == strtoupper($method)) {
  32. curl_setopt($ch, CURLOPT_URL, "$url?$query_string");
  33. } else {
  34. curl_setopt($ch, CURLOPT_URL, $url);
  35. curl_setopt($ch, CURLOPT_POST, 1);
  36. curl_setopt($ch, CURLOPT_POSTFIELDS, $query_string);
  37. }
  38. curl_setopt($ch, CURLOPT_HEADER, false);
  39. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  40. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
  41. // disable 100-continue
  42. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
  43. if (!empty($cookie_string)) {
  44. curl_setopt($ch, CURLOPT_COOKIE, $cookie_string);
  45. }
  46. if ('https' == $protocol) {
  47. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  48. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  49. }
  50. $ret = curl_exec($ch);
  51. $err = curl_error($ch);
  52. if (false === $ret || !empty($err)) {
  53. $errno = curl_errno($ch);
  54. $info = curl_getinfo($ch);
  55. curl_close($ch);
  56. return array(
  57. 'result' => false,
  58. 'errno' => $errno,
  59. 'msg' => $err,
  60. 'info' => $info,
  61. );
  62. }
  63. curl_close($ch);
  64. return array(
  65. 'result' => true,
  66. 'msg' => $ret,
  67. );
  68. }
  69. /**
  70. * 执行一个 HTTP 请求,以post方式,multipart/form-data的编码类型上传文件
  71. *
  72. * @param string $url 执行请求的URL
  73. * @param mixed $params 表单参数,必须是array, 对于文件表单项 直接传递文件的全路径, 并在前面增加'@'符号
  74. * 举例: array('upload_file'=>'@/home/xxx/hello.jpg', 'field1'=>'value1');
  75. * @param mixed $cookie cookie参数
  76. * 可以是array, 也可以是经过拼接的string
  77. * @param string $protocol http协议类型 http / https
  78. * @return array 结果数组
  79. */
  80. public static function makeRequestWithFile($url, $params, $cookie, $protocol='http')
  81. {
  82. $cookie_string = self::makeCookieString($cookie);
  83. $ch = curl_init();
  84. curl_setopt($ch, CURLOPT_URL, $url);
  85. curl_setopt($ch, CURLOPT_POST, 1);
  86. curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  87. curl_setopt($ch, CURLOPT_HEADER, false);
  88. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  89. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
  90. // disable 100-continue
  91. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
  92. if (!empty($cookie_string)) {
  93. curl_setopt($ch, CURLOPT_COOKIE, $cookie_string);
  94. }
  95. if ('https' == $protocol) {
  96. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  97. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  98. }
  99. $ret = curl_exec($ch);
  100. $err = curl_error($ch);
  101. if (false === $ret || !empty($err)) {
  102. $errno = curl_errno($ch);
  103. $info = curl_getinfo($ch);
  104. curl_close($ch);
  105. return array(
  106. 'result' => false,
  107. 'errno' => $errno,
  108. 'msg' => $err,
  109. 'info' => $info,
  110. );
  111. }
  112. curl_close($ch);
  113. return array(
  114. 'result' => true,
  115. 'msg' => $ret,
  116. );
  117. }
  118. public static function makeQueryString($params)
  119. {
  120. if (is_string($params))
  121. return $params;
  122. $query_string = array();
  123. foreach ($params as $key => $value) {
  124. array_push($query_string, rawurlencode($key) . '=' . rawurlencode($value));
  125. }
  126. $query_string = join('&', $query_string);
  127. return $query_string;
  128. }
  129. public static function makeCookieString($params)
  130. {
  131. if (is_string($params))
  132. return $params;
  133. $cookie_string = array();
  134. foreach ($params as $key => $value) {
  135. array_push($cookie_string, $key . '=' . $value);
  136. }
  137. $cookie_string = join('; ', $cookie_string);
  138. return $cookie_string;
  139. }
  140. }
  141. // end of script