我想知道如何通過WHMCS API獲得WHMCS價格。WHCS價格通過API動態數組
我已經創建了一個腳本來通過API工作正常,但當我更新我的外部WHMCS頁面上的腳本,它需要更多的時間來更新每個頁面上的功能,我的簡單問題是價格通過API而不定義第一個數組請參見下面的示例。
Array
(
[result] => success
[totalresults] => xxx
[products] => Array
(
[product] => Array
(
[0] => Array // how can I call it dynamically
(
[pid] => 1
[gid] => 1
[type] => hostingaccount
[name] => Plan Name
[description] => Plan Description
[module] => cpanel
[paytype] => recurring
[pricing] => Array
(
[USD] => Array
(
[prefix] => $
[suffix] => USD
[msetupfee] => 0.00
[qsetupfee] => 0.00
[ssetupfee] => 0.00
[asetupfee] => 0.00
[bsetupfee] => 0.00
[tsetupfee] => 0.00
[monthly] => 0.14
[quarterly] => 0.39
[semiannually] => 0.73
[annually] => 1.32
[biennially] => 2.39
[triennially] => 3.20
)
)
)
)
)
)
我只是想獲得的價格後,我定義[PID],然後我將創建像
GetPrice($pid, $billingcycle); // to get produce price according to tenure
功能我的腳本:
$identifier = "IDENTIFIER";
$secret = "SECRET";
$APIURL = "mydomain/whmcs_directory/includes/api.php"; // it is with HTTPS
$postfields = array(
'username' => $identifier,
'password' => $secret,
'action' => 'GetProducts',
'responsetype' => 'json',
);
// Call the API
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $APIURL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postfields));
$response = curl_exec($ch);
if (curl_error($ch)) {
die('Unable to connect: ' . curl_errno($ch) . ' - ' . curl_error($ch));
}
curl_close($ch);
// Decode response
$jsonData = json_decode($response, true);
,然後我想使用函數來獲得價格根據產品編號&定義如前定義,但補全te函數看起來像這樣。
function GetPrice($pid, $billingcycle){
// $pid will be product ID
// $billingcycle will be 1,3,6,12,24,36 accordingly.
/**
* It would be great if I could just remove "["products"]["product"]"
* But I understand to call API I have define them so it's Okay.
*/
$monthly = $jsonData["products"]["product"][$pid]["pricing"]["USD"]["monthly"];
$quarterly = $jsonData["products"]["product"][$pid]["pricing"]["USD"]["quarterly"];
$semiannually = $jsonData["products"]["product"][$pid]["pricing"]["USD"]["semiannually"];
$annually = $jsonData["products"]["product"][$pid]["pricing"]["USD"]["annually"];
$biennially = $jsonData["products"]["product"][$pid]["pricing"]["USD"]["biennially"];
$triennially = $jsonData["products"]["product"][$pid]["pricing"]["USD"]["triennially"];
if($billingcycle == "1" ){
echo $monthly;
}
if($billingcycle == "3" ){
echo $quarterly;
}
if($billingcycle == "6" ){
echo $semiannually;
}
if($billingcycle == "12" ){
echo $annually;
}
if($billingcycle == "24" ){
echo $biennially;
}
if($billingcycle == "36" ){
echo $triennially;
}
}
我得到的幫助來自WHMCS API Reference
這與PHP做,請提高我的代碼,如果需要。
您可以將PID相關數組保存到一個新的variab le,像這樣: $ productDetails = $ jsonData [「products」] [「product」] [$ pid]; then: $ monthly = $ productDetails [「pricing」] [「USD」] [「monthly」]; – wesamly
@wesamly感謝花時間審查的主要原因是在這裏提出問題,擺脫產品下的第一陣列讓我更新我的代碼,以便更好地瞭解 – shaz3e
代碼剛剛更新請看看 – shaz3e