ZQDcsApiService.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. 'PageNum'=>"-1",
  33. 'PointIds' => $pointIds,
  34. ]);
  35. // 获取 API 响应的 JSON 数据
  36. $res = json_decode($response->getBody(), true);
  37. if($res['Errors']) {
  38. return null;
  39. }
  40. $transformedData = $this->transformData($res);
  41. return $transformedData;
  42. }
  43. // public function postApiData($apiUrl, $postData)
  44. // {
  45. // // 发送 HTTP POST 请求
  46. // $response = $this->client->post($apiUrl, [
  47. // 'PageNum'=>"-1",
  48. // 'form_params' => $postData,
  49. // // 可以添加其他选项,如 headers、auth 等
  50. // ]);
  51. //
  52. // // 获取 API 响应的 JSON 数据
  53. // $data = json_decode($response->getBody(), true);
  54. //
  55. // // 对数据进行重组,这里只是一个示例,具体根据实际需求调整
  56. // $transformedData = $this->transformData($data);
  57. //
  58. // return $transformedData;
  59. // }
  60. protected function transformData($data)
  61. {
  62. $transformedData=[];
  63. for ($i = 0; $i < count($data); $i++) {
  64. $dic = $data[$i];
  65. $pid = $dic['ID'];
  66. $val = $dic['V'];
  67. $transformedData[$pid]=$val;
  68. }
  69. return $transformedData;
  70. }
  71. }