dadaRequestClient.php 3.9 KB

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