ZQDcsApiService.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. return $data;
  21. }
  22. public function postPointRealData($apiUrl, $pointIds) {
  23. // 发送 HTTP POST 请求
  24. $response = $this->client->post($apiUrl, [
  25. 'headers' => [
  26. 'Authorization' => 'Bearer appkey_100100',
  27. 'Content-Type'=>'application/json; charset=utf-8',
  28. // 其他头部信息
  29. ],
  30. 'json' =>[
  31. 'PageNum'=>"-1",
  32. 'PointIds' => $pointIds
  33. ]
  34. ]);
  35. // 获取 API 响应的 JSON 数据
  36. $res = json_decode($response->getBody(), true);
  37. if($res['IsSuccessful'] == 0) {
  38. return $res;
  39. }
  40. $data = isset($res['Data'])? $res['Data']: null;
  41. $transformedData['data'] = $this->transformData($data);
  42. $transformedData['IsSuccessful'] =1;
  43. return $transformedData;
  44. }
  45. public function postApiData($apiUrl, $postData = '')
  46. {
  47. // 发送 HTTP POST 请求
  48. $response = $this->client->post($apiUrl, $postData);
  49. // 发送 HTTP POST 请求
  50. $response = $this->client->post($apiUrl, [
  51. 'headers' => [
  52. 'Authorization' => 'Bearer appkey_100100',
  53. 'Content-Type'=>'application/json; charset=utf-8',
  54. // 其他头部信息
  55. ],
  56. 'json' =>[
  57. 'PageNum'=>"-1"
  58. ]
  59. ]);
  60. return $response;
  61. // 获取 API 响应的 JSON 数据
  62. $data = json_decode($response->getBody(), true);
  63. // 对数据进行重组,这里只是一个示例,具体根据实际需求调整
  64. $transformedData = $this->transformData($data);
  65. return $transformedData;
  66. }
  67. protected function transformData($data)
  68. {
  69. $transformedData=[];
  70. for ($i = 0; $i < count($data); $i++) {
  71. $dic = $data[$i];
  72. $pid = $dic['ID'];
  73. $val = $dic['V'];
  74. $transformedData[$pid]=$val;
  75. }
  76. return $transformedData;
  77. }
  78. }