Zscaler API Example Code

1.2k views Asked by At

I have been trying to connect to the zscalershift API: https://api.zscalershift.net/ I was able to use the web based swagger style API calls, but I was struggling to get things working in postman or PHP. I figure others will have this issue as well and thought it would be worthwhile to post this online.

1

There are 1 answers

0
TMNetworks On

Ok, here is some example code of how to authenticate to the API, then add a site using a function in PHP or update a location using a function.

$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);
    }
}


?>