PHP 传输会话curl函数的实例详解

PHP 传输会话curl函数的实例详解

前言:接手公司项目PC端负责人的重担,责任担当重大;从需求分析,画流程图,建表,编码,测试修bug,上线维护等我一个光杆司令一人完成(当然还有一个技术不错的前端配合,感谢主管的帮助),虽然累点加班多点但感觉还行吧,公司都是一个鸟样。

闲话不多说了,因为项目中经常需要调取java那边的接口,既然涉及到请求接口那就有了http的请求方式,PHP常见的是GET/POST两种当然还有其他的比如put等,java那边经常用到GET/POST/PUT/DELETE等方式,请求接口当然要用到curl的相关函数了,都是看文档调试的希望大家都看文档,下面是我封装好的相关函数等(大概总结下,已调通):

示例代码:

  1. private $serverhost = "https://demo.xxx.cn"; //测试
  2. /**
  3. * 请求接口封装 get/post/put/delete等
  4. * access public
  5. * @param string $url 接口地址
  6. * @param string $params 参数
  7. * @param string $type 类型 get/post/put/delete
  8. * @return bool/array
  9. */
  10. public function getcurldata($url,$params,$type="get"){
  11. $url = $this->serverhost.$url;
  12. $response = array();
  13. if($type == 'get'){ //get请求
  14. //请求头可以加其他设置
  15. $headers = array(
  16. 'Content-type: application/json;charset=UTF-8',
  17. );
  18. $ch = curl_init();
  19. curl_setopt($ch, CURLOPT_URL, $url);
  20. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  21. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
  22. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  23. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  24. $response = curl_exec($ch);
  25. }elseif ($type == 'post'){ //post请求
  26. $headers = array(
  27. 'Content-type: application/json;charset=UTF-8',
  28. );
  29. $ch = curl_init();
  30. curl_setopt($ch, CURLOPT_URL, $url);
  31. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  32. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
  33. curl_setopt($ch, CURLOPT_POST, true); //注意这几行
  34. curl_setopt($ch, CURLOPT_POSTFIELDS, $params); //注意这几行
  35. //curl_setopt($ch, CURLOPT_HEADER, true);
  36. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  37. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  38. $response = curl_exec($ch);
  39. }elseif ($type == 'put'){ //put请求
  40. $headers = array(
  41. 'Content-type: application/json;charset=UTF-8',
  42. );
  43. $ch = curl_init();
  44. curl_setopt($ch, CURLOPT_URL, $url);
  45. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  46. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
  47. curl_setopt($ch, CURLOPT_PUT, true); //注意这几行
  48. curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  49. //curl_setopt($ch, CURLOPT_HEADER, true);
  50. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  51. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  52. $response = curl_exec($ch);
  53. }
  54. return $response;
  55. }
  56. //如何调用上面代码
  57. //get方式
  58. /**
  59. * 查询我创建过的班级
  60. * @param string $url 接口地址
  61. * @param string $params 参数
  62. * @param string $type 类型 get
  63. * @return array
  64. */
  65. public function mycreateclass($userid){
  66. $url = "/xxx/xxxx/xxxx/".$userid; //请求地址拼接
  67. $response = $this->getcurldata($url,array(),"get");
  68. $createdclass = json_decode($response, true); //返回json格式数据
  69. return $createdclass;
  70. }
  71. /** post方式请求
  72. * 用户登录判断
  73. * access public
  74. * @param string $username 用户名
  75. * @param string $password 密码
  76. * @return bool
  77. */
  78. public function getlogin($username,$password)
  79. {
  80. //要post的数据
  81. $params = array(
  82. "username" => $username,
  83. "password" => $password
  84. );
  85. $params = json_encode($params, 64|256);
  86. $uri = "/xxx/xxx/login";
  87. $response = $this->getcurldata($uri,$params,"post");
  88. $result = json_decode($response, true);
  89. return $result ;
  90. }
  91. /*身份转换--put 请求
  92. */
  93. public function changeuserole($token){
  94. //要put的数据
  95. $params = array();
  96. $params = json_encode($params, 64|256);
  97. $uri = "/xxx/xxx/xxx/".$token."/";
  98. $response = $this->getcurldata($uri,$params,"put");
  99. $result = json_decode($response, true);
  100. //dump($result);die;
  101. return $result;
  102. }
  103. //还有一个delete方式 大家自己参考文档调试下吧
  104. 上面3个请求方式都是单次请求(即请求一次)***************
  105. PHP自带函数curl_multi_get_contents函数(thinkphp自带此函数,可以微调下):
  106. /**
  107. * 批量发起请求 已调通
  108. * curl multi POST数据
  109. * */
  110. public function curl_multi_get_contents($url=array(), $param = array(), $timeout=1000){
  111. $userAgent = 'Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.1;+SV1)';
  112. $headers = array(
  113. 'Content-type: application/json;charset=UTF-8',
  114. );
  115. $curl_array=array();
  116. $mh = curl_multi_init();
  117. foreach($url as $uk=>$uv){
  118. $curl_array[$uk] = curl_init();
  119. }
  120. unset($uk,$uv);
  121. foreach($url as $uk=>$uv) {
  122. $options = array(
  123. CURLOPT_URL => $uv,
  124. CURLOPT_TIMEOUT => $timeout,
  125. CURLOPT_RETURNTRANSFER => true,
  126. CURLOPT_DNS_CACHE_TIMEOUT => 86400,
  127. CURLOPT_DNS_USE_GLOBAL_CACHE => true,
  128. CURLOPT_SSL_VERIFYPEER => 0,
  129. CURLOPT_HEADER => false,
  130. CURLOPT_USERAGENT => 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)',
  131. CURLOPT_HTTPHEADER => $headers,
  132. );
  133. if (isset($param[$uk])) {
  134. foreach ($param[$uk] as $_k => $_v) {
  135. //$options[$_k] = $_v;
  136. $optionsparam[$_k] = $_v;
  137. $options[CURLOPT_POSTFIELDS] = json_encode($optionsparam, 64|256);
  138. }
  139. }
  140. curl_setopt_array($curl_array[$uk], $options);
  141. curl_multi_add_handle($mh, $curl_array[$uk]);
  142. unset($options);
  143. }
  144. unset($uk,$uv);
  145. $running = NULL;
  146. do {
  147. curl_multi_exec($mh,$running);
  148. } while($running > 0);
  149. $res = array();
  150. foreach($url as $uk=>$uv){
  151. $res[$uk] = curl_multi_getcontent($curl_array[$uk]);
  152. }
  153. unset($uk,$uv);
  154. foreach($url as $uk=>$uv){
  155. curl_multi_remove_handle($mh, $curl_array[$uk]);
  156. }
  157. unset($url,$curl_array,$uk,$uv);
  158. curl_multi_close($mh);
  159. return $res;
  160. }
  161. //如何调用--批量发起请求
  162. //批量请求加入班级
  163. public function batchjoinclass($token,$batchjoinclass){
  164. $urlarr = $param = $returndata = array();
  165. $param = $batchjoinclass; //二维数组 格式如下
  166. /*
  167. $param[1]['name'] = '班级新1';
  168. $param[1]['iamge'] = 'xxx11.png';
  169. $param[1]['iamgeType'] = 'CUSTOM';
  170. $param[2]['name'] = '班级新2';
  171. $param[2]['iamge'] = 'xxx.png';
  172. $param[2]['iamgeType'] = 'CUSTOM';
  173. */
  174. //获取请求url
  175. foreach($batchjoinclass as $key=>$val){
  176. $urlarr[$key] = $this->serverhost."/xxx/xxxx/xxxx/".$token;
  177. } //phpfensi.com
  178. $res = $this->curl_multi_get_contents($urlarr,$param);
  179. //<a href="http://www.111cn.net/zhuanti/geshihua/" class="anchor" target="_blank">格式化</a>返回数据
  180. foreach($res as $key=>$val){
  181. $returndata[$key] = json_decode($val,true);
  182. }
  183. return $returndata;
  184. }