首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Zscaler API示例代码

Zscaler API示例代码
EN

Stack Overflow用户
提问于 2017-09-12 06:25:25
回答 1查看 816关注 0票数 0

我一直在尝试连接zscalershift API: PHP我可以使用基于web的swagger风格的API调用,但我很难在postman或https://api.zscalershift.net/中工作。我认为其他人也会有这个问题,并认为这将是值得在网上张贴这一点。

EN

回答 1

Stack Overflow用户

发布于 2017-09-12 06:25:25

好的,下面是一些示例代码,演示如何向API进行身份验证,然后使用PHP中的函数添加站点或使用函数更新位置。

代码语言:javascript
复制
$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);
    }
}


?>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46165153

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档