dadaRequestClient.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * 达达接口请求客户端
  4. */
  5. $dada = Yii::getAlias("@vendor/dada/");
  6. require_once $dada . 'client/dadaResponse.php';
  7. require_once $dada . 'config/constant.php';
  8. class DadaRequestClient
  9. {
  10. /**
  11. * http request timeout;
  12. */
  13. private $httpTimeout = 5;
  14. /**
  15. * 配置项
  16. */
  17. private $config;
  18. /**
  19. * 接口类
  20. */
  21. private $api;
  22. /**
  23. * 构造函数
  24. */
  25. public function __construct($config, $api)
  26. {
  27. $this->config = $config;
  28. $this->api = $api;
  29. }
  30. /**
  31. * 请求调用api
  32. * @return bool
  33. */
  34. public function makeRequest()
  35. {
  36. $reqParams = $this->bulidRequestParams();
  37. $resp = $this->getHttpRequestWithPost(json_encode($reqParams));
  38. return $this->parseResponseData($resp);
  39. }
  40. /**
  41. * 构造请求数据
  42. * data:业务参数,json字符串
  43. */
  44. public function bulidRequestParams()
  45. {
  46. $config = $this->getConfig();
  47. $api = $this->getApi();
  48. $requestParams = array();
  49. $requestParams['app_key'] = $config->getAppKey();
  50. $requestParams['body'] = $api->getBusinessParams();
  51. $requestParams['format'] = $config->getFormat();
  52. $requestParams['v'] = $config->getV();
  53. $requestParams['source_id'] = $config->getSourceId();
  54. $requestParams['timestamp'] = time();
  55. $requestParams['signature'] = $this->_sign($requestParams);
  56. return $requestParams;
  57. }
  58. /**
  59. * 签名生成signature
  60. */
  61. public function _sign($data)
  62. {
  63. $config = $this->getConfig();
  64. //1.升序排序
  65. ksort($data);
  66. //2.字符串拼接
  67. $args = "";
  68. foreach ($data as $key => $value) {
  69. $args .= $key . $value;
  70. }
  71. $args = $config->app_secret . $args . $config->app_secret;
  72. //3.MD5签名,转为大写
  73. $sign = strtoupper(md5($args));
  74. return $sign;
  75. }
  76. /**
  77. * 发送请求,POST
  78. * @param $url 指定URL完整路径地址
  79. * @param $data 请求的数据
  80. */
  81. public function getHttpRequestWithPost($data)
  82. {
  83. $config = $this->config;
  84. $api = $this->api;
  85. $url = $config->getHost() . $api->getUrl();
  86. // json
  87. $headers = array(
  88. 'Content-Type: application/json',
  89. );
  90. $curl = curl_init($url);
  91. curl_setopt($curl, CURLOPT_URL, $url);
  92. curl_setopt($curl, CURLOPT_HEADER, false);
  93. curl_setopt($curl, CURLOPT_POST, true);
  94. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  95. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  96. curl_setopt($curl, CURLOPT_TIMEOUT, 3);
  97. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  98. $resp = curl_exec($curl);
  99. var_dump(curl_error($curl));//如果在执行curl的过程中出现异常,可以打开此开关查看异常内容。
  100. $info = curl_getinfo($curl);
  101. curl_close($curl);
  102. if (isset($info['http_code']) && $info['http_code'] == 200) {
  103. return $resp;
  104. }
  105. return '';
  106. }
  107. /**
  108. * 解析响应数据
  109. * @param $arr返回的数据
  110. * 响应数据格式:{"status":"success","result":{},"code":0,"msg":"成功"}
  111. */
  112. public function parseResponseData($arr)
  113. {
  114. $resp = new DadaResponse();
  115. if (empty($arr)) {
  116. $resp->setStatus(DadaConstant::FAIL);
  117. $resp->setMsg(DadaConstant::FAIL_MSG);
  118. $resp->setCode(DadaConstant::FAIL_CODE);
  119. } else {
  120. $data = json_decode($arr, true);
  121. $resp->setStatus($data['status']);
  122. $resp->setMsg($data['msg']);
  123. $resp->setCode($data['code']);
  124. $result = isset($data['result']) ? $data['result'] : '';
  125. $resp->setResult($result);
  126. }
  127. return $resp;
  128. }
  129. public function getConfig()
  130. {
  131. return $this->config;
  132. }
  133. public function getApi()
  134. {
  135. return $this->api;
  136. }
  137. }