# 接口客户端使用示例(PHP):


<?php

/**
 * 猫酷开放平台接入示例
 * Class Mallcoo
 * @package tool\mallcoo
 */ 
class Mallcoo
{
    public $m_Mallid = '';
    public $m_AppID = '';
    public $m_PublicKey = '';
    public $m_PrivateKey = '';
 
    /**
     * 初始化
     *
     * @param string $sMallid Mallid
     * @param string $sAppID AppID
     * @param string $sPublicKey 公钥
     * @param string $sPrivateKey 私钥
     */
    public function __construct($sMallid, $sAppID, $sPublicKey, $sPrivateKey){
        $this->m_Mallid = $sMallid;
        $this->m_AppID = $sAppID;
        $this->m_PublicKey = $sPublicKey;
        $this->m_PrivateKey = $sPrivateKey;
    }
 
    /**
     * 获取授权url
     *
     * @param string $sCallbackUrl 回调url
     * @return string
     */
    public function getOauthUrl($sCallbackUrl){
        $sUrl = 'https://m.mallcoo.cn/a/open/User/V2/OAuth/CardInfo/';
        $sUrl .= '?AppID='.$this->m_AppID.'&PublicKey='.$this->m_PublicKey.'&CallbackUrl='.urlencode($sCallbackUrl);
        return $sUrl;
    }
 
    /**
     * 通过 Ticket 获取 Token
     *
     * @param string $sTicket Ticket
     * @return
     */
    public function GetTokenByTicket($sTicket){
        $sUrl = 'https://openapi10.mallcoo.cn/User/OAuth/v1/GetToken/ByTicket/';
        return $this->mallcooPost($sUrl, array('Ticket' => $sTicket));
    }
 
    /**
     * mallcoo post 请求
     *
     * @param string $sUrl 请求url
     * @param array $aPostData 请求参数
     * @return array
     */
    private function mallcooPost($sUrl, $aPostData){
        $sPostData = json_encode($aPostData);
        $nTimeStamp = date('YmdHis',time());
        $sS = "{publicKey:".$this->m_PublicKey.",timeStamp:".$nTimeStamp.",data:".$sPostData.",privateKey:".$this->m_PrivateKey."}";
        $sSign = strtoupper(substr(md5($sS), 8, 16));
        $aHeader = array(
            'Content-Type: application/json; charset=utf-8',
            'Content-Length: ' . strlen($sPostData),
            'AppID: '.$this->m_AppID,
            'TimeStamp: '.$nTimeStamp,
            'PublicKey: '.$this->m_PublicKey,
            'Sign: '.$sSign,
        );
        $sR = $this->curl_post($sUrl, $aHeader, $sPostData);
        return json_decode(html_entity_decode($sR), true);
    }
 
    /**
     * curl post 请求
     *
     * @param string $url
     * @param array $aHeader
     * @param string $sParams
     * @param string $cookie
     * @return string
     */
    private function curl_post($url, $aHeader, $sParams, $cookie='') {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $sParams);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        $result = curl_exec($ch);
        if ($result === false){
            // log curl_error($ch);
        }
        curl_close($ch);
        return $result;
    }
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
Last Updated: 2019/10/15 上午11:34:20