Curl.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /* file: curl请求类
  3. Created by wanghong<1204772286@qq.com>
  4. Date: 2021-02-22 */
  5. namespace utils;
  6. class Curl{
  7. /*
  8. 请求封装-curl_request
  9. $url -string 请求地址
  10. $method -string 请求方式,默认GET
  11. $headers -array 请求头,默认[]
  12. $bodys -array 请求体,默认[]
  13. $json -boolean 对请求体进行json_encode处理,默认false
  14. return $response 请求返回值
  15. */
  16. public static function curl_request($url, $method = 'GET', $headers = [], $bodys = [], $json=false)
  17. {
  18. if($json==false){
  19. $bodys=json_encode($bodys);
  20. }
  21. // 创建连接
  22. $curl = curl_init($url);
  23. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);//跳过证书检查
  24. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
  25. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  26. curl_setopt($curl, CURLOPT_FAILONERROR, false);
  27. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  28. curl_setopt($curl, CURLOPT_HEADER, false);
  29. curl_setopt($curl, CURLOPT_POST, true);
  30. curl_setopt($curl, CURLOPT_POSTFIELDS, $bodys);
  31. // 发送请求
  32. $response = curl_exec($curl);
  33. if($json && is_string($response)){
  34. $response=json_decode($response,true);
  35. }
  36. curl_close($curl);
  37. return $response;
  38. }
  39. /*
  40. get请求-curl_get
  41. $url -string 请求地址
  42. $json -boolean 对返回值进行json_decode处理,默认true进行处理成array
  43. */
  44. public static function curl_get($url,$json=true)
  45. {
  46. $curl = curl_init();
  47. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);//跳过证书检查、
  48. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
  49. curl_setopt($curl, CURLOPT_URL, $url);
  50. curl_setopt($curl, CURLOPT_HTTPHEADER, array("Expect:"));
  51. curl_setopt($curl, CURLOPT_FAILONERROR, false);
  52. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  53. curl_setopt($curl, CURLOPT_HEADER, false);
  54. $return = curl_exec($curl);
  55. curl_close ( $curl );
  56. if($json){
  57. return json_decode($return, true);
  58. }else{
  59. return $return;
  60. }
  61. }
  62. /*
  63. POST请求-curl_post
  64. $url -string 请求地址
  65. $params - json 请求参数
  66. $rj -boolean 对返回值进行json_decode处理,默认true进行处理成array
  67. $headers -string 请求头
  68. */
  69. public static function curl_post($url,$params,$rj=true,$headers=''){
  70. if(!$headers){
  71. $headers=array(
  72. "Content-Type:application/x-www-form-urlencoded",
  73. );
  74. }
  75. $ch = curl_init ();
  76. curl_setopt ( $ch, CURLOPT_URL, $url );
  77. curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
  78. curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, 'POST' );
  79. curl_setopt ( $ch, CURLOPT_POSTFIELDS, $params );
  80. curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
  81. curl_setopt ( $ch, CURLOPT_TIMEOUT, 60 );
  82. $result = curl_exec ( $ch );
  83. curl_close ( $ch );
  84. if($rj){
  85. return json_decode($result,true);
  86. }else{
  87. return $result;
  88. }
  89. }
  90. }