ZQDcsApiService.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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, [
  51. // 'PageNum'=>"-1",
  52. // 'form_params' => $postData,
  53. // // 可以添加其他选项,如 headers、auth 等
  54. // ]);
  55. //
  56. // // 获取 API 响应的 JSON 数据
  57. // $data = json_decode($response->getBody(), true);
  58. //
  59. // // 对数据进行重组,这里只是一个示例,具体根据实际需求调整
  60. // $transformedData = $this->transformData($data);
  61. //
  62. // return $transformedData;
  63. // }
  64. protected function transformData($data)
  65. {
  66. $transformedData=[];
  67. for ($i = 0; $i < count($data); $i++) {
  68. $dic = $data[$i];
  69. $pid = $dic['ID'];
  70. $val = $dic['V'];
  71. $transformedData[$pid]=$val;
  72. }
  73. return $transformedData;
  74. }
  75. }