TestService.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace App\Service;
  3. class TestService extends Service
  4. {
  5. public function testdwy($data){
  6. if(empty($data['url']) || empty($data['post']) || empty($data['header'])) return [false,'API请求参数不能为空'];
  7. $url = $data['url'];
  8. $post = $data['post'];
  9. $header = $data['header'];
  10. $json = json_encode($post);
  11. $json = str_replace('"workflowSearchBean":[]','"workflowSearchBean":{}',$json);
  12. $json = str_replace('"loginBindingParameters":[]','"loginBindingParameters":{}',$json);
  13. list($status, $result) = $this->post_helper($url,$json, $header);
  14. if(! $status) return [false, $result];
  15. return [true, $result];
  16. }
  17. public function testdwyget($data){
  18. if(empty($data['url']) || empty($data['header'])) return [false,'API请求参数不能为空'];
  19. $url = $data['url'];
  20. $header = $data['header'];
  21. list($status,$result) = $this->get_helper($url,$header);
  22. if(! $status) return [false, $result];
  23. return [true, $result];
  24. }
  25. public function testdwyput($data){
  26. if(empty($data['url']) || empty($data['post']) || empty($data['header'])) return [false,'API请求参数不能为空'];
  27. $url = $data['url'];
  28. $post = $data['post'];
  29. $header = $data['header'];
  30. $json = json_encode($post);
  31. $json = str_replace('"workflowSearchBean":{}','"workflowSearchBean":[]',json_encode($post));
  32. $json = str_replace('"workflowSearchBean":[]','"workflowSearchBean":{}',json_encode($post));
  33. list($status, $result) = $this->put_helper($url,$json, $header);
  34. if(! $status) return [false, $result];
  35. return [true, $result];
  36. }
  37. public function post_helper($url, $data, $header = [], $timeout = 20){
  38. $file_name = 'record_test' . date("Y-m-d") . '.txt';
  39. file_put_contents($file_name,date('Y-m-d H:i:s') . PHP_EOL. "请求API:" . $url . PHP_EOL . "请求参数:" . $data . PHP_EOL . "请求头部:" . json_encode($header) . PHP_EOL,8);
  40. $ch = curl_init();
  41. curl_setopt($ch, CURLOPT_URL, $url);
  42. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  43. curl_setopt($ch, CURLOPT_ENCODING, '');
  44. curl_setopt($ch, CURLOPT_POST, 1);
  45. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  46. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  47. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  48. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  49. if(!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  50. $r = curl_exec($ch);
  51. if ($r === false) {
  52. // 获取错误号
  53. $errorNumber = curl_errno($ch);
  54. // 获取错误信息
  55. $errorMessage = curl_error($ch);
  56. return [false, "cURL Error #{$errorNumber}: {$errorMessage}"];
  57. }
  58. curl_close($ch);
  59. file_put_contents($file_name,date('Y-m-d H:i:s') . PHP_EOL . "返回结果:" . $r . PHP_EOL,8);
  60. return [true, json_decode($r, true)];
  61. }
  62. public function get_helper($url,$header=[],$timeout = 20){
  63. $file_name = 'record_test' . date("Y-m-d") . '.txt';
  64. $ch = curl_init();
  65. curl_setopt_array($ch, array(
  66. CURLOPT_URL => $url,
  67. CURLOPT_RETURNTRANSFER => true,
  68. CURLOPT_ENCODING => '',
  69. CURLOPT_MAXREDIRS => 10,
  70. CURLOPT_TIMEOUT => $timeout,
  71. CURLOPT_FOLLOWLOCATION => true,
  72. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  73. CURLOPT_CUSTOMREQUEST => 'GET',
  74. CURLOPT_SSL_VERIFYPEER => false,
  75. CURLOPT_HTTPHEADER => $header,
  76. ));
  77. $r = curl_exec($ch);
  78. if ($r === false) {
  79. // 获取错误号
  80. $errorNumber = curl_errno($ch);
  81. // 获取错误信息
  82. $errorMessage = curl_error($ch);
  83. return [false, "cURL Error #{$errorNumber}: {$errorMessage}"];
  84. }
  85. curl_close($ch);
  86. file_put_contents($file_name,date('Y-m-d H:i:s') . PHP_EOL . "GET返回结果:" . $r . PHP_EOL,8);
  87. return [true, json_decode($r, true)];
  88. }
  89. public function put_helper($url, $data, $header = [], $timeout = 20){
  90. $file_name = 'record_test' . date("Y-m-d") . '.txt';
  91. file_put_contents($file_name,date('Y-m-d H:i:s') . PHP_EOL. "请求API:" . $url . PHP_EOL . "请求参数:" . $data . PHP_EOL . "请求头部:" . json_encode($header) . PHP_EOL,8);
  92. $ch = curl_init();
  93. curl_setopt($ch, CURLOPT_URL, $url);
  94. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  95. curl_setopt($ch, CURLOPT_ENCODING, '');
  96. curl_setopt($ch, CURLOPT_POST, 1);
  97. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
  98. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  99. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  100. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  101. if(!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  102. $r = curl_exec($ch);
  103. if ($r === false) {
  104. // 获取错误号
  105. $errorNumber = curl_errno($ch);
  106. // 获取错误信息
  107. $errorMessage = curl_error($ch);
  108. return [false, "cURL Error #{$errorNumber}: {$errorMessage}"];
  109. }
  110. curl_close($ch);
  111. file_put_contents($file_name,date('Y-m-d H:i:s') . PHP_EOL . "返回结果:" . $r . PHP_EOL,8);
  112. return [true, json_decode($r, true)];
  113. }
  114. }