Kwaixiaodian.php 6.0 KB

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