wupengfei 1 gadu atpakaļ
vecāks
revīzija
03d876587f

+ 58 - 0
application/api/controller/Kuaishou.php

@@ -0,0 +1,58 @@
+<?php
+namespace app\api\controller;
+class Kuaishou
+{
+    // md5 签名
+    public static function md5Sign($param,$signSecret)
+    {
+        $param = [
+            'appkey'=> '',
+            'timestamp'=> '',
+            'access_token'=> '',
+            'version'=> 1,
+            'param'=> json_encode(['user_id'=>1,'order_num'=>'123456']),
+            'method' => 'MD5',
+        ];
+        $paramStr = '';
+        ksort($param);
+        foreach ($param as $kk=>$vv) {
+            if($vv != null && 'null' != $vv)
+            {
+                $paramStr .=$kk.'='.$vv.'&';
+            }
+        }
+        $unSignParaString = '';
+        if(strlen($paramStr)> 0) $unSignParaString = substr($paramStr,0,strlen($paramStr) - 1);
+        return  md5($unSignParaString.'&signSecret='.$signSecret);
+    }
+    // 2.HMAC_SHA256(官方推荐)
+    public static function HMAC_SHA256Sign($param,$signSecret){
+        $paramStr = "";
+        ksort($param);
+        foreach ($param as $kk=>$vv){
+            if (null != $vv && "null" != $vv) {
+                $paramStr .= $kk . "=" . $vv . "&";
+            }
+        }
+        $unSignParaString = '';
+        if (strlen($paramStr) > 0) {
+            $unSignParaString = substr($paramStr, 0, strlen($paramStr) - 1);
+        }
+        $unSignParaString = $unSignParaString."&signSecret=" . $signSecret;
+        $signStr = base64_encode(
+            hash_hmac(
+                'sha256',
+                $unSignParaString,
+                $signSecret,
+                true
+            )
+        );
+        return $signStr;
+    }
+
+}
+
+
+
+
+

+ 155 - 0
application/api/controller/Kwaixiaodian.php

@@ -0,0 +1,155 @@
+<?php
+ini_set("display_errors", 0);
+date_default_timezone_set("Asia/Shanghai");
+error_reporting(E_ALL);
+set_time_limit(0);
+header("Content-type: text/html; charset=utf-8");
+$Kwaixiaodian = new Kwaixiaodian();
+$Kwaixiaodian->appKey = $appKey = 'appKey ';
+$Kwaixiaodian->appSecret = $appSecret = 'appSecret ';
+$Kwaixiaodian->signSecret = 'signSecret ';
+$Kwaixiaodian->debug = 1;;
+$Kwaixiaodian->access_token = 'access_token';
+$arr = $Kwaixiaodian->api('open.item.list.get', array());
+//$arr =$Kwaixiaodian->api('open.user.seller.get',array());
+echo '<pre>';
+print_r($arr);
+echo '</pre>';
+
+
+class Kwaixiaodian {
+    public $appKey;
+    public $appSecret;
+    public $signSecret;
+    public $syncAPIClient;
+    public $serverHost = "https://open.kwaixiaodian.com/";
+    public $webSite = '1688';
+    public $access_token = '';
+    public $debug = false;
+
+    public function Api($apiType, $param, $method = 'get') {
+        $arr['appkey'] = $this->appKey;
+        $arr['version'] = '1';
+        $arr['access_token'] = $this->access_token;
+        $arr['timestamp'] = $this->getMillisecond();
+        $arr['method'] = trim($apiType);
+        $arr['param'] = $param ? json_encode($param) : '{}';
+        $arr['signMethod'] = 'MD5';
+        ksort($arr); // 排序
+        $arr['sign'] = $this->getSign($arr, $this->signSecret);
+        $apiInfo = str_replace('.', '/', $arr['method']);
+        $url = $this->serverHost . $apiInfo;
+        $Alibabahelper = new Alibabahelper();
+        if ($method == 'get') {
+            $s = $Alibabahelper->curl_https_get($url. '?' . http_build_query($arr, '', '&'), array());
+        } else {
+            $s = $Alibabahelper->curl_https_post($url, $arr);
+        }
+        $s = json_decode($s, true);
+        return $s;
+    }
+    public function getSign($params, $key) {
+        $unSignParaString = $this->formatQueryParaMap($params, false);
+        $signStr = (md5($unSignParaString . "&signSecret=" . $this->signSecret));
+        return $signStr;
+    }
+
+    public function formatQueryParaMap(array $paraMap, $urlEncode = false) {
+        $buff = "";
+        ksort($paraMap);
+        foreach ($paraMap as $k => $v) {
+            if (null != $v && "null" != $v) {
+                if ($urlEncode) {
+                    $v = urlencode($v);
+                }
+                $buff.= $k . "=" . $v . "&";
+            }
+        }
+        $reqPar = '';
+        if (strlen($buff) > 0) {
+            $reqPar = substr($buff, 0, strlen($buff) - 1);
+        }
+        return $reqPar;
+    }
+
+    /*获取13位时间戳*/
+    private static function getMillisecond() {
+        list($t1, $t2) = explode(' ', microtime());
+        return sprintf('%.0f', (floatval($t1) + floatval($t2)) * 1000);
+    }
+
+    public function getAPIClient($redirectUrl,$scope='merchant_comment,merchant_item,merchant_logistics,merchant_order,merchant_servicemarket,merchant_user,user_info') {
+
+        $get_code_url = $this->serverHost . "oauth2/connect?app_id={$this->appKey}&scope={$scope}&response_type=code&redirect_uri={$redirectUrl}&state=your_state";
+
+        return $get_code_url;
+    }
+
+    public function getToken($code, $redirectUrl = '') {
+        $url = $this->serverHost . "oauth2/access_token?app_id={$this->appKey}&app_secret={$this->appSecret}&code={$code}&grant_type=authorization_code";
+        $Alibabahelper = new Alibabahelper();
+        $s = $Alibabahelper->curl_https_get($url);
+        $s = json_decode($s, true);
+        return $s;
+    }
+    /*用长时令牌refreshToken刷新访问令牌accessToken*/
+    public function refreshToken($refreshToken) {
+        $url = $this->serverHost . "oauth2/refresh_token?app_id={$this->appKey}&app_secret={$this->appSecret}&refresh_token={$refreshToken}&grant_type=refresh_token";
+        $Alibabahelper = new Alibabahelper();
+        $s = $Alibabahelper->curl_https_get($url);
+        $s = json_decode($s, true);
+        return $s;
+    }
+
+    function debug($msg) {
+        if ($this->debug) {
+            print '<div style="border: 1px solid red; padding: 0.5em; margin: 0.5em;">';
+            echo "<pre>";
+            print_r($msg);
+            echo "</pre>";
+            print '</div>';
+        }
+    }
+}
+class Alibabahelper {
+    function __construct() {
+    }
+    private function curl_get_contents($url, $data = array(), $https = false) {
+        $results['error'] = '';
+        $results['status'] = 0;
+        $results['data'] = array();
+        $user_agent = $_SERVER['HTTP_USER_AGENT'];
+        $curl = curl_init(); // 启动一个CURL会话
+        if (!empty($data) && is_array($data)) {
+            curl_setopt($curl, CURLOPT_POST, TRUE);
+            curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
+            curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
+        }
+        if ($https) {
+            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
+            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); // 从证书中检查SSL加密算法是否存在
+            curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
+
+        }
+        curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
+        curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
+        curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
+        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
+        curl_setopt($curl, CURLOPT_USERAGENT, $user_agent); // 模拟用户使用的浏览器
+        curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
+        $results['data'] = curl_exec($curl); // 执行操作
+        if (curl_errno($curl)) {
+            $results['error'] = curl_error($curl); //捕抓异常
+
+        }
+        curl_close($curl); // 关闭CURL会话
+        return $results['data']; // 返回数据
+
+    }
+    public function curl_https_post($url, $data) {
+        return $this->curl_get_contents($url, $data, true);
+    }
+    public function curl_https_get($url) {
+        return $this->curl_get_contents($url, array(), true);
+    }
+}