PHP語法範例

範例檔下載
        
                <?php

                //建立時間戳  時間是utc+8
                    $timestamp = time()+8*3600;
                    echo $timestamp;
                    echo "<br>";

                //廠商編號
                    $pronum = 'TMSDEMOWEB';
                //Api加密金鑰 
                    $apikey = '66E10461-249A-4A26-B846-180B2F4E3839';

                //Api內容 
                    $body = '{
                                "ProNum":"TMSDEMOWEB",
                                "Main":{
            	                    "CCODE" : "",
            	                    "AutoCCODE": 1,
            	                    "CNAM2" : "王小明",
            	                    "CNAM1" : "王小明",
            	                    "CADD1": "台中市XX路XXX號",
            	                    "CTELE": "04-24222299",
            	                    "CPOS1": "111",
            	                    "CBIRD": "1973-01-01", 
            	                    "CTEL2": "04-99999999",
            	                    "CPhone": "0933555666",
            	                    "EditItem": "0"}
                              }';

                //加密字串  將 廠商,時間戳,API內容 相連 
                    $SignStr = $pronum . $timestamp . $body;

                    echo $SignStr;
                    echo "<br>";

               //進行加密
                    $ClientSign = hash_hmac('sha512', $SignStr, $apikey);

                    echo $ClientSign;

                    echo "<br>";

               //Api網址
                    $url = "http://tms-api.ktnet.com.tw/Cust/Add";

               //Request Header內容
                    $headers = [
                        'api-pno: ' . $pronum ,
                        'api-timestamp: ' . $timestamp,
                        'api-ClientSign: ' . $ClientSign
                    ];

               //進行POST
                    $ch = curl_init();
                    curl_setopt($ch, CURLOPT_URL, $url);
                    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
                    curl_setopt($ch, CURLOPT_POST, true);
                    curl_setopt($ch, CURLOPT_POSTFIELDS, $body); 
                    $output = curl_exec($ch); 
                    curl_close($ch);

               //取得回傳值
                    echo $output;

                    ?>