WHMCS APIを使用してWHMCS価格を取得する方法を知りたい。WHCS動的配列を使用したAPIによる価格
APIを使用して価格を入手するための小さなスクリプトを作成しましたが、外部のWHMCSページでスクリプトを更新すると、各ページの関数を更新するのにもっと時間がかかります。最初の配列を定義せずにAPIを使用して価格を計算します。その後、
$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);
と:私はちょうど私が定義した後の価格を取得したい
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
のように私のスクリプトを関数を作成します私は前に定義されているように製品id &の在任期間に応じて価格を得るために関数を使用したい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;
}
}
私はこれはPHPで行っているWHMCS API Reference
からの助けを得た必要な場合、私のコードを改善してください。
新しいvariabにPID関連配列を保存することができますleのように: $ productDetails = $ jsonData ["products"] ["product"] [$ pid]; : $ monthly = $ productDetails ["USD"] ["monthly"]; – wesamly
@wesamly主な理由を確認する時間を費やしているのは、製品の下にある最初の配列を取り除くためにここに質問してください。 – shaz3e
コードがちょうど更新されました – shaz3e