2017-07-16 3 views
1

Je veux savoir comment obtenir le prix WHMCS via l'API WHMCS.WHCS Prix via API avec tableau dynamique

J'ai créé un petit script pour obtenir le prix via l'API qui fonctionne très bien mais quand je mets à jour le script sur mes pages externes WHMCS il faut beaucoup plus de temps pour mettre à jour la fonction sur chaque page. prix via API sans définir le premier tableau voir l'exemple ci-dessous.

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 
             ) 

           ) 

         ) 
       ) 
     ) 
) 

Je veux juste obtenir le prix après que je définis [pid], puis je vais créer une fonction comme

GetPrice($pid, $billingcycle); // to get produce price according to tenure

Mon script:

$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); 

puis Je veux utiliser la fonction pour obtenir le prix selon l'ID de produit & tenure comme défini plus tôt, mais comple La fonction ressemble à ceci.

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; 
    } 
} 

J'obtenu l'aide de WHMCS API Reference

Cela a fait avec php, s'il vous plaît améliorer mon code si besoin.

+0

Vous pouvez enregistrer le tableau lié pid dans une nouvelle variab le, comme ceci: $ productDetails = $ jsonData ["products"] ["product"] [$ pid]; puis: $ mensuel = $ productDetails ["pricing"] ["USD"] ["mensuel"]; – wesamly

+0

@wesamlyMerci de passer du temps à revoir la raison principale est de poser une question ici pour se débarrasser de la première matrice sous produit laissez-moi mettre à jour mon code pour mieux comprendre – shaz3e

+0

Code vient de mettre à jour s'il vous plaît jeter un oeil – shaz3e

Répondre

2

Je l'ai réalisé avec le code suivant qui fonctionne dynamiquement et fonctionne avec l'ID de produit.

PHP Fonction

function GetPrice($product_id, $billingcycle){ 

    $identifier = "WHMCS_IDENTIFIER"; 
    $secret = "WHMCS_SECRET"; 
    $APIURL = "YOURDOMAIN.com/WHMCS_DIRECTORY/includes/api.php"; // use HTTPS 

    $postfields = array(
     'username'  => $identifier, 
     'password'  => $secret, 
     'action'  => 'GetProducts', 
     'pid'   => $product_id, // Product id 
     '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); // SET to 0 for non SSL 
    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)); 
     die("Out of Stock"); 
    } 
    curl_close($ch); 

    // Decode response 
    $jsonData = json_decode($response, true); 

    $monthly  = $jsonData["products"]["product"][0]["pricing"]["USD"]["monthly"]; 
    $quarterly  = $jsonData["products"]["product"][0]["pricing"]["USD"]["quarterly"]; 
    $semiannually = $jsonData["products"]["product"][0]["pricing"]["USD"]["semiannually"]; 
    $annually  = $jsonData["products"]["product"][0]["pricing"]["USD"]["annually"]; 
    $biennially  = $jsonData["products"]["product"][0]["pricing"]["USD"]["biennially"]; 
    $triennially = $jsonData["products"]["product"][0]["pricing"]["USD"]["triennially"]; 

    if($billingcycle == "1" ){ 
     echo round(str_replace('.00', '', $monthly), 2); 
    } 

    if($billingcycle == "3" ){ 
     echo round(str_replace('.00', '', $quarterly)/3, 2); 
    } 

    if($billingcycle == "6" ){ 
     echo round(str_replace('.00', '', $semiannually)/6, 2); 
    } 

    if($billingcycle == "12" ){ 
     echo round(str_replace('.00', '', $annually)/12, 2); 
    } 

    if($billingcycle == "24" ){ 
     echo round(str_replace('.00', '', $biennially)/24, 2); 
    } 

    if($billingcycle == "36" ){ 
     echo round(str_replace('.00', '', $triennially)/36, 2); 
    } 
} 

Utilisation de la fonction

echo GetPrice(1 , 1); 

Sortie

1.99 
+0

Bien fait @ shaz3e :) – wesamly

+0

Merci, @wesamly est-ce que mon code a besoin d'être amélioré? – shaz3e