【问题标题】:Zscaler API Example CodeZscaler API 示例代码
【发布时间】:2017-09-12 06:25:25
【问题描述】:

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

【问题讨论】:

    标签: php rest api zscaler


    【解决方案1】:

    好的,这里有一些示例代码,说明如何通过 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);
        }
    }
    
    
    ?>
    

    【讨论】:

      猜你喜欢
      • 2016-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-02
      • 1970-01-01
      • 2016-05-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多