Kuaishou.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace app\api\controller;
  3. class Kuaishou extends Base
  4. {
  5. public $appKey = 'ks698620896473026758';
  6. public $appSecret = 'cvbOgiVC6rSvPs0sBASfkg';
  7. public $signSecret = '71443462ca217e1c29425e7aabc257cc';
  8. public $msgSecret = '4fxDQv7tJKzIc1D7CNNe6A==';
  9. public $syncAPIClient;
  10. public $serverHost = "https://open.kwaixiaodian.com/";
  11. public $webSite = '1688';
  12. public $access_token = '';
  13. public $debug = false;
  14. //
  15. public function getAccessToken()
  16. {
  17. //$url = $this->serverHost . "oauth2/access_token?app_id={$this->appKey}&app_secret={$this->appSecret}&code={$code}&grant_type=authorization_code";
  18. $url = $this->serverHost . "oauth2/access_token?app_id={$this->appKey}&app_secret={$this->appSecret}&grant_type=client_credentials";
  19. $Alibabahelper = new Alibabahelper();
  20. $s = $Alibabahelper->curl_https_get($url);
  21. $s = json_decode($s, true);
  22. return $s['access_token'];
  23. }
  24. // api请求
  25. public function apiSend()
  26. {
  27. $apiType = "open.order.detail";
  28. $method = 'get';
  29. $param = ['oid'=>'2315900153213958'];
  30. $arr['appkey'] = $this->appKey;
  31. $arr['version'] = '1';
  32. $arr['access_token'] = $this->getAccessToken();
  33. $arr['timestamp'] = $this->getMillisecond();
  34. $arr['method'] = trim($apiType);
  35. $arr['param'] = $param ? json_encode($param) : '{}';
  36. $arr['signMethod'] = 'MD5';
  37. ksort($arr); // 排序
  38. $arr['sign'] = $this->getSign($arr, $this->signSecret);
  39. $apiInfo = str_replace('.', '/', $arr['method']);
  40. $url = $this->serverHost . $apiInfo;
  41. $Alibabahelper = new Alibabahelper();
  42. var_dump($arr);
  43. if ($method == 'get') {
  44. $s = $Alibabahelper->curl_https_get($url. '?' . http_build_query($arr, '', '&'), array());
  45. } else {
  46. $s = $Alibabahelper->curl_https_post($url, $arr);
  47. }
  48. $s = json_decode($s, true);
  49. $this->success('ok',$s);
  50. }
  51. public function getSign($params, $key) {
  52. $unSignParaString = $this->formatQueryParaMap($params, false);
  53. $signStr = (md5($unSignParaString . "&signSecret=" . $this->signSecret));
  54. return $signStr;
  55. }
  56. public function formatQueryParaMap(array $paraMap, $urlEncode = false) {
  57. $buff = "";
  58. ksort($paraMap);
  59. foreach ($paraMap as $k => $v) {
  60. if (null != $v && "null" != $v) {
  61. if ($urlEncode) {
  62. $v = urlencode($v);
  63. }
  64. $buff.= $k . "=" . $v . "&";
  65. }
  66. }
  67. $reqPar = '';
  68. if (strlen($buff) > 0) {
  69. $reqPar = substr($buff, 0, strlen($buff) - 1);
  70. }
  71. return $reqPar;
  72. }
  73. /*获取13位时间戳*/
  74. private static function getMillisecond() {
  75. list($t1, $t2) = explode(' ', microtime());
  76. return sprintf('%.0f', (floatval($t1) + floatval($t2)) * 1000);
  77. }
  78. }
  79. class Alibabahelper {
  80. function __construct() {
  81. }
  82. private function curl_get_contents($url, $data = array(), $https = false) {
  83. $results['error'] = '';
  84. $results['status'] = 0;
  85. $results['data'] = array();
  86. $user_agent = $_SERVER['HTTP_USER_AGENT'];
  87. $curl = curl_init(); // 启动一个CURL会话
  88. if (!empty($data) && is_array($data)) {
  89. curl_setopt($curl, CURLOPT_POST, TRUE);
  90. curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
  91. curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
  92. }
  93. if ($https) {
  94. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
  95. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在
  96. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
  97. }
  98. curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
  99. curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
  100. curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
  101. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
  102. curl_setopt($curl, CURLOPT_USERAGENT, $user_agent); // 模拟用户使用的浏览器
  103. curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
  104. $results['data'] = curl_exec($curl); // 执行操作
  105. if (curl_errno($curl)) {
  106. $results['error'] = curl_error($curl); //捕抓异常
  107. }
  108. curl_close($curl); // 关闭CURL会话
  109. return $results['data']; // 返回数据
  110. }
  111. public function curl_https_post($url, $data) {
  112. return $this->curl_get_contents($url, $data, true);
  113. }
  114. public function curl_https_get($url) {
  115. return $this->curl_get_contents($url, array(), true);
  116. }
  117. }