我一直在尝试连接到 zscalershift API:https ://api.zscalershift.net/ 我能够使用基于 Web 的 swagger 风格的 API 调用,但我很难让事情在邮递员或 PHP 中工作。我认为其他人也会遇到这个问题,并认为值得将其发布到网上。
问问题
875 次
1 回答
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);
}
}
?>
于 2017-09-11T22:25:25.157 回答