Zscaler API示例代码

时间:2017-09-11 22:25:25

标签: php rest api zscaler

我一直在尝试连接到zscalershift API:https://api.zscalershift.net/ 我能够使用基于Web的swagger样式API调用,但我很难让邮件或PHP工作。 我认为其他人也会遇到这个问题,并认为在网上发布这个问题是值得的。

1 个答案:

答案 0 :(得分:0)

好的,这里有一些示例代码,说明如何对API进行身份验证,然后使用PHP中的函数添加站点或使用函数更新位置。

$ZScalerUser = 'USERNAME';
$ZScalerPass = 'PASSWORD';
$CustomerId = 'CUSTOMERID';
$SiteName = 'SITENAME';
$ZSLocId ='LOCATIONID';
$ZSIpAddr = 'IPADDRESS';

$ZScalerAuthCurl = ZScalerSignIn($ZScalerUser,$ZScalerPass);
ZScalerAddLocation($ZScalerAuthCurl,$CustomerId,$SiteName,$ZSIpAddr);
ZScalerUpdateLocation($ZScalerAuthCurl,$CustomerId,$SiteName,$ZSLocId,$ZSIpAddr);


function ZScalerSignIn($ZScalerUser,$ZScalerPass) {
    $curl = curl_init();

    curl_setopt_array($curl, array(
        CURLOPT_URL => "https://api.zscalershift.net/shift/api/v1/signin",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POST => true,
        CURLOPT_COOKIESESSION => true,
        CURLOPT_COOKIEJAR => 'myjar',
        CURLOPT_COOKIEFILE => 'cookie',
        CURLOPT_POSTFIELDS => "username=$ZScalerUser&password=$ZScalerPass",
        CURLOPT_HTTPHEADER => array(
            "content-type: application/x-www-form-urlencoded"
        ),
    ));

    $ZScalerAuthResponse = curl_exec($curl);
    $err = curl_error($curl);

    if ($err) {
        echo "cURL Error #:" . $err;
    } else {
        $ZScalerAuthDecoded = json_decode($ZScalerAuthResponse, true);
        print_r($ZScalerAuthDecoded);
        //echo $ZScalerAuthDecoded['Z-AUTH-TOKEN'];
        return($curl);
    }
}

function ZScalerAddLocation($ZScalerAuthCurl,$CustomerId,$SiteName,$ZSIpAddr) {
    curl_setopt_array($ZScalerAuthCurl, array(
        CURLOPT_URL => "https://api.zscalershift.net/shift/api/v1/admin/customers/$CustomerId/locations",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_POSTFIELDS => "{
            \"ipAddresses\": [\"$ZSIpAddr\"],
            \"locationType\": \"STATIC\",
            \"name\": \"$SiteName\",
            \"tagAssociations\": [\"PROFILETAG\"]
        }",
        CURLOPT_HTTPHEADER => array(
            "Accept: */*",
            "content-type: application/json"
        ),
    ));

    $response = curl_exec($ZScalerAuthCurl);
    $err = curl_error($ZScalerAuthCurl);

    if ($err) {
        echo "cURL Error #:" . $err;
    } else {
        $decoded_response = json_decode($response, true);
        print_r($decoded_response);
    }
}


function ZScalerUpdateLocation($ZScalerAuthCurl,$CustomerId,$SiteName,$ZSLocId,$ZSIpAddr) {
    curl_setopt_array($ZScalerAuthCurl, array(
        CURLOPT_URL => "https://api.zscalershift.net/shift/api/v1/admin/customers/$CustomerId/locations/$ZSLocId",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "PUT",
        CURLOPT_POSTFIELDS => "{
            \"name\": \"$SiteName\",
            \"ipAddresses\": [\"$ZSIpAddr\"]
        }",
        CURLOPT_HTTPHEADER => array(
            "Accept: */*",
            "content-type: application/json"
        ),
    ));

    $response = curl_exec($ZScalerAuthCurl);
    $err = curl_error($ZScalerAuthCurl);

    if ($err) {
        echo "cURL Error #:" . $err;
    } else {
        $decoded_response = json_decode($response, true);
        print_r($decoded_response);
    }
}


?>