ZQDcsApiService.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace Modules\OpcData\Services\zaoquan;
  3. use GuzzleHttp\Client;
  4. class ZQDcsApiService
  5. {
  6. protected $client;
  7. public function __construct()
  8. {
  9. $this->client = new Client();
  10. }
  11. public function getApiData($apiUrl, $queryParams)
  12. {
  13. // 发送 HTTP GET 请求
  14. $response = $this->client->get($apiUrl, [
  15. 'query' => $queryParams,
  16. // 可以添加其他选项,如 headers、auth 等
  17. ]);
  18. // 获取 API 响应的 JSON 数据
  19. $data = json_decode($response->getBody(), true);
  20. // 对数据进行重组,这里只是一个示例,具体根据实际需求调整
  21. $transformedData = $this->transformData($data);
  22. return $transformedData;
  23. }
  24. public function postPointRealData($apiUrl, $pointIds) {
  25. // 发送 HTTP POST 请求
  26. $response = $this->client->post($apiUrl, [
  27. 'headers' => [
  28. 'Authorization' => 'Bearer appkey_100100',
  29. 'Content-Type'=>'application/json; charset=utf-8',
  30. // 其他头部信息
  31. ],
  32. 'json' =>[
  33. 'PageNum'=>"-1",
  34. 'PointIds' => $pointIds
  35. ]
  36. ]);
  37. // 获取 API 响应的 JSON 数据
  38. $res = json_decode($response->getBody(), true);
  39. if($res['IsSuccessful'] == 0) {
  40. return $res;
  41. }
  42. $data = isset($res['Data'])? $res['Data']: null;
  43. $transformedData['data'] = $this->transformData($data);
  44. $transformedData['IsSuccessful'] =1;
  45. return $transformedData;
  46. }
  47. public function postApiData($apiUrl, $postData = '')
  48. {
  49. // 发送 HTTP POST 请求
  50. $response = $this->client->post($apiUrl, $postData);
  51. // 获取 API 响应的 JSON 数据
  52. $data = json_decode($response->getBody(), true);
  53. // 对数据进行重组,这里只是一个示例,具体根据实际需求调整
  54. $transformedData = $this->transformData($data);
  55. return $transformedData;
  56. }
  57. protected function transformData($data)
  58. {
  59. $transformedData=[];
  60. for ($i = 0; $i < count($data); $i++) {
  61. $dic = $data[$i];
  62. $pid = $dic['ID'];
  63. $val = $dic['V'];
  64. $transformedData[$pid]=$val;
  65. }
  66. return $transformedData;
  67. }
  68. }