Kwaixiaodian.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. ini_set("display_errors", 0);
  3. date_default_timezone_set("Asia/Shanghai");
  4. error_reporting(E_ALL);
  5. set_time_limit(0);
  6. header("Content-type: text/html; charset=utf-8");
  7. $Kwaixiaodian = new Kwaixiaodian();
  8. $Kwaixiaodian->appKey = $appKey = 'ks698620896473026758';
  9. $Kwaixiaodian->appSecret = $appSecret = 'cvbOgiVC6rSvPs0sBASfkg';
  10. $Kwaixiaodian->signSecret = '71443462ca217e1c29425e7aabc257cc';
  11. $Kwaixiaodian->msgSecret = '4fxDQv7tJKzIc1D7CNNe6A==';// 消息秘钥
  12. $Kwaixiaodian->debug = 1;
  13. $Kwaixiaodian->access_token = 'access_token';
  14. $arr = $Kwaixiaodian->api('open.item.list.get', array());
  15. //$arr =$Kwaixiaodian->api('open.user.seller.get',array());
  16. echo '<pre>';
  17. print_r($arr);
  18. echo '</pre>';
  19. class Kwaixiaodian {
  20. public $appKey;
  21. public $appSecret;
  22. public $signSecret;
  23. public $syncAPIClient;
  24. public $serverHost = "https://open.kwaixiaodian.com/";
  25. public $webSite = '1688';
  26. public $access_token = '';
  27. public $debug = false;
  28. public function Api($apiType, $param, $method = 'get') {
  29. $arr['appkey'] = $this->appKey;
  30. $arr['version'] = '1';
  31. $arr['access_token'] = $this->access_token;
  32. $arr['timestamp'] = $this->getMillisecond();
  33. $arr['method'] = trim($apiType);
  34. $arr['param'] = $param ? json_encode($param) : '{}';
  35. $arr['signMethod'] = 'MD5';
  36. ksort($arr); // 排序
  37. $arr['sign'] = $this->getSign($arr, $this->signSecret);
  38. $apiInfo = str_replace('.', '/', $arr['method']);
  39. $url = $this->serverHost . $apiInfo;
  40. $Alibabahelper = new Alibabahelper();
  41. if ($method == 'get') {
  42. $s = $Alibabahelper->curl_https_get($url. '?' . http_build_query($arr, '', '&'), array());
  43. } else {
  44. $s = $Alibabahelper->curl_https_post($url, $arr);
  45. }
  46. $s = json_decode($s, true);
  47. return $s;
  48. }
  49. public function getSign($params, $key) {
  50. $unSignParaString = $this->formatQueryParaMap($params, false);
  51. $signStr = (md5($unSignParaString . "&signSecret=" . $this->signSecret));
  52. return $signStr;
  53. }
  54. public function formatQueryParaMap(array $paraMap, $urlEncode = false) {
  55. $buff = "";
  56. ksort($paraMap);
  57. foreach ($paraMap as $k => $v) {
  58. if (null != $v && "null" != $v) {
  59. if ($urlEncode) {
  60. $v = urlencode($v);
  61. }
  62. $buff.= $k . "=" . $v . "&";
  63. }
  64. }
  65. $reqPar = '';
  66. if (strlen($buff) > 0) {
  67. $reqPar = substr($buff, 0, strlen($buff) - 1);
  68. }
  69. return $reqPar;
  70. }
  71. /*获取13位时间戳*/
  72. private static function getMillisecond() {
  73. list($t1, $t2) = explode(' ', microtime());
  74. return sprintf('%.0f', (floatval($t1) + floatval($t2)) * 1000);
  75. }
  76. public function getAPIClient($redirectUrl,$scope='merchant_comment,merchant_item,merchant_logistics,merchant_order,merchant_servicemarket,merchant_user,user_info') {
  77. $get_code_url = $this->serverHost . "oauth2/connect?app_id={$this->appKey}&scope={$scope}&response_type=code&redirect_uri={$redirectUrl}&state=your_state";
  78. return $get_code_url;
  79. }
  80. public function getToken($code, $redirectUrl = '') {
  81. $url = $this->serverHost . "oauth2/access_token?app_id={$this->appKey}&app_secret={$this->appSecret}&code={$code}&grant_type=authorization_code";
  82. $Alibabahelper = new Alibabahelper();
  83. $s = $Alibabahelper->curl_https_get($url);
  84. $s = json_decode($s, true);
  85. return $s;
  86. }
  87. /*用长时令牌refreshToken刷新访问令牌accessToken*/
  88. public function refreshToken($refreshToken) {
  89. $url = $this->serverHost . "oauth2/refresh_token?app_id={$this->appKey}&app_secret={$this->appSecret}&refresh_token={$refreshToken}&grant_type=refresh_token";
  90. $Alibabahelper = new Alibabahelper();
  91. $s = $Alibabahelper->curl_https_get($url);
  92. $s = json_decode($s, true);
  93. return $s;
  94. }
  95. function debug($msg) {
  96. if ($this->debug) {
  97. print '<div style="border: 1px solid red; padding: 0.5em; margin: 0.5em;">';
  98. echo "<pre>";
  99. print_r($msg);
  100. echo "</pre>";
  101. print '</div>';
  102. }
  103. }
  104. }
  105. class Alibabahelper {
  106. function __construct() {
  107. }
  108. private function curl_get_contents($url, $data = array(), $https = false) {
  109. $results['error'] = '';
  110. $results['status'] = 0;
  111. $results['data'] = array();
  112. $user_agent = $_SERVER['HTTP_USER_AGENT'];
  113. $curl = curl_init(); // 启动一个CURL会话
  114. if (!empty($data) && is_array($data)) {
  115. curl_setopt($curl, CURLOPT_POST, TRUE);
  116. curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
  117. curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
  118. }
  119. if ($https) {
  120. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
  121. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); // 从证书中检查SSL加密算法是否存在
  122. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
  123. }
  124. curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
  125. curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
  126. curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
  127. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
  128. curl_setopt($curl, CURLOPT_USERAGENT, $user_agent); // 模拟用户使用的浏览器
  129. curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
  130. $results['data'] = curl_exec($curl); // 执行操作
  131. if (curl_errno($curl)) {
  132. $results['error'] = curl_error($curl); //捕抓异常
  133. }
  134. curl_close($curl); // 关闭CURL会话
  135. return $results['data']; // 返回数据
  136. }
  137. public function curl_https_post($url, $data) {
  138. return $this->curl_get_contents($url, $data, true);
  139. }
  140. public function curl_https_get($url) {
  141. return $this->curl_get_contents($url, array(), true);
  142. }
  143. }