---
# System prepended metadata

title: PHP Curl Code
tags: [Advanced Authentication]

---

# PHP Curl Code

<?php
        $url = "https://aa.colorlife.com.tw/api/v1/endpoints/{endpoint_id}/sessions";
        $endpoint_secret_hash = "{endpoint_secret_hash}";
        $timenow = date("Y-m-d H:i:s");
        $timemark = array("data" => "'$timenow'");
        $data2 = array(
            "salt" => "i_am_salt",
            "endpoint_secret_hash" => $endpoint_secret_hash,
            "session_data" => array(
                "timemark" => $timemark
            ),
            "os" => array(
                "display_name" => "CentOS 7",
                "platform" => "x64",
                "version" => "3.10.0-1062.12.1.el7"
            ),
            "software_type" => "BuiltinOSP",
            "software_ver" => "6.2",
            "name" => "support"
        );
        $endpoint = curl_init();

        curl_setopt($endpoint, CURLOPT_URL, $url);
        curl_setopt($endpoint, CURLOPT_POST, true);
        curl_setopt($endpoint, CURLOPT_POSTFIELDS, http_build_query($data2));
        curl_setopt($endpoint, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($endpoint, CURLOPT_HEADER, false);
        $EndpointSession_ID = curl_exec($endpoint);
        $EndpointSession = json_decode($EndpointSession_ID, true);
        $EndpointSession_ID = $EndpointSession['endpoint_session_id'];
        curl_close($endpoint);


 
        
        //進行登錄
        $url1 = "https://aa.colorlife.com.tw/api/v1/logon";
        $data = array(

            "method_id" => "SMARTPHONE:1",
            "user_name" => "$username",
            "event" => "support",
            "endpoint_session_id" => "$EndpointSession_ID"

        );
        $logon1 = curl_init();
        curl_setopt($logon1, CURLOPT_FORBID_REUSE, 0);
        curl_setopt($logon1, CURLOPT_URL, $url1);
        curl_setopt($logon1, CURLOPT_POST, true);
        curl_setopt($logon1, CURLOPT_POSTFIELDS, http_build_query($data));
        curl_setopt($logon1, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($logon1, CURLOPT_HEADER, false);
        curl_setopt($logon1, CURLOPT_HTTPHEADER, array(
            'Connection: Keep-Alive',
            'Keep-Alive: 300'
        ));
        $logon_process_ID = curl_exec($logon1);
        $logon_process = json_decode($logon_process_ID, true);
        $logon_status = $logon_process['status'];
        $logon_process_ID = $logon_process['logon_process_id'];

        if ($logon_status == "error") {
            echo '<script>alert("請先向管理員註冊或帳號輸入錯誤！！")</script>';
        } else {
            $url2 = "https://aa.colorlife.com.tw/api/v1/logon/$logon_process_ID/do_logon";
            $data1 = array(
                "method_id" => "SMARTPHONE:1",
                "endpoint_session_id" => "$EndpointSession_ID"
            );
            curl_setopt($logon1, CURLOPT_URL, $url2);
            curl_setopt($logon1, CURLOPT_POST, true);
            curl_setopt($logon1, CURLOPT_POSTFIELDS, http_build_query($data1));
            curl_setopt($logon1, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($logon1, CURLOPT_HEADER, false);
            $output1 = curl_exec($logon1);
            $status_json = json_decode($output1, true);
            $status = $status_json['status'];
            sleep(5);
            while ($status == "MORE_DATA") {
                $url2 = "https://aa.colorlife.com.tw/api/v1/logon/$logon_process_ID/do_logon";
                $data1 = array(
                    "method_id" => "SMARTPHONE:1",
                    "endpoint_session_id" => "$EndpointSession_ID"
                );
                curl_setopt($logon1, CURLOPT_URL, $url2);
                curl_setopt($logon1, CURLOPT_POST, true);
                curl_setopt($logon1, CURLOPT_POSTFIELDS, http_build_query($data1));
                curl_setopt($logon1, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($logon1, CURLOPT_HEADER, false);
                $output1 = curl_exec($logon1);

                $status_json = json_decode($output1, true);
                $status = $status_json['status'];
                sleep(3);
            }
           

            if ($status == "OK") {
        ?>
                <form id="loginForm" name="loginForm" action="index.php" method="post">
                    <div class="textbox">
                        <i class="fas fa-user"></i>
                        <input type="hidden" name="aanameOK" value="OK" />
                    </div>
                </form>
                <script type="text/javascript">
                    document.loginForm.submit();
                </script>
        <?php
            }
            echo "驗證成功後，若無反應，請重整頁面";
            if ($status == "error") {
                echo '<script>alert("請重新登入")</script>';
            }
        }

        ?>
        
        
###### tags: `Advanced Authentication`