OPCDataTurboController.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace Modules\OpcData\Http\Controllers\Api;
  3. use App\Http\Controllers\Api\BaseController;
  4. use Illuminate\Contracts\Support\Renderable;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Routing\Controller;
  7. class OPCDataTurboController extends BaseController
  8. {
  9. public function getData(Request $request) {
  10. $sys_key = $request->sys_key;
  11. $sys_name = $request->sys_name;
  12. if (!isset($sys_key) || !isset($sys_name)) {
  13. return $this->error(-1, '参数错误');
  14. }
  15. $db_conf = config('database');
  16. $api_url = 'http://'.$db_conf['python_api'][$sys_key]['url'].'/get/?sys_key='.$sys_key.'&sys_name='.$sys_name;
  17. $response = $this->request_post($api_url);
  18. if (gettype($response) == 'integer') {
  19. switch ($response) {
  20. case 3:
  21. $error_info = 'CURLE_URL_MALFORMAT';
  22. break;
  23. case 7:
  24. $error_info = 'CURLE_COULDNT_CONNECT';
  25. break;
  26. default:
  27. $error_info = '未知错误:'.$response;
  28. }
  29. return $this->error($response, $error_info);
  30. } else {
  31. $data = json_decode($response);
  32. if ($data == null) {
  33. return $this->error(500, preg_replace('/<[^>]*>/','',$response));
  34. } else {
  35. return $this->success($data);
  36. }
  37. }
  38. }
  39. private function request_post($url = '', $param = '') {
  40. if (empty($url)) {
  41. return false;
  42. }
  43. $postUrl = $url;
  44. $curlPost = $param;
  45. $curl = curl_init();//初始化curl
  46. curl_setopt($curl, CURLOPT_URL,$postUrl);//抓取指定网页
  47. curl_setopt($curl, CURLOPT_HEADER, 0);//设置header
  48. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
  49. curl_setopt($curl, CURLOPT_POST, 1);//post提交方式
  50. curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);//提交的参数
  51. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  52. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  53. // curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  54. $data = curl_exec($curl);//运行curl
  55. $errno = curl_errno($curl);
  56. curl_close($curl);
  57. if ($errno > 0) return $errno;
  58. return $data;
  59. }
  60. /**
  61. * Display a listing of the resource.
  62. * @return Renderable
  63. */
  64. public function index()
  65. {
  66. return view('opcdata::index');
  67. }
  68. /**
  69. * Show the form for creating a new resource.
  70. * @return Renderable
  71. */
  72. public function create()
  73. {
  74. return view('opcdata::create');
  75. }
  76. /**
  77. * Store a newly created resource in storage.
  78. * @param Request $request
  79. * @return Renderable
  80. */
  81. public function store(Request $request)
  82. {
  83. //
  84. }
  85. /**
  86. * Show the specified resource.
  87. * @param int $id
  88. * @return Renderable
  89. */
  90. public function show($id)
  91. {
  92. return view('opcdata::show');
  93. }
  94. /**
  95. * Show the form for editing the specified resource.
  96. * @param int $id
  97. * @return Renderable
  98. */
  99. public function edit($id)
  100. {
  101. return view('opcdata::edit');
  102. }
  103. /**
  104. * Update the specified resource in storage.
  105. * @param Request $request
  106. * @param int $id
  107. * @return Renderable
  108. */
  109. public function update(Request $request, $id)
  110. {
  111. //
  112. }
  113. /**
  114. * Remove the specified resource from storage.
  115. * @param int $id
  116. * @return Renderable
  117. */
  118. public function destroy($id)
  119. {
  120. //
  121. }
  122. }