skip to Main Content

I want to be able to extract each network plan with prices from this GET API. Can someone please help me out?

I want to Extract AIRTEL DATA PLAN [dataPlan, duration,type, status, and Price for basic_user] into a select Option format.

I have done this

  $url = "https://subandgain.com/api/databundles.php";
    $curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
curl_close($curl);
$obj = json_decode($response,true);
$suppliers = array();
$customers = array();
//echo "<pre>";
//print_r($obj);
foreach ($obj as $rkey => $resource){

    if ($resource['NETWORK'] == 'AIRTEL'){

        $customers[] = $resource;

    } else if ($resource['NETWORK'] == 'MTN') {

        $suppliers[] = $resource;

    }

}

header('Content-Type: application/json');
$dataplan = json_encode($customers);
$objs = json_decode($dataplan,true);
echo "<pre>";
print_r($objs);

Please Help me out

2

Answers


  1. The $objs variable is returning json_decode. Replace with json_encode. So I made adjustments and PHP got this format.

    <?php
    $url = "https://subandgain.com/api/databundles.php";
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($curl);
    curl_close($curl);
    $obj = json_decode($response,true);
    $customers = array();
    $suppliers = array();
    
    foreach ($obj as $rkey => $resource){
        switch($resource['NETWORK']){
            case "AIRTEL":
                $customers[] = $resource;
                break;
            case "MTN":
                $suppliers[] = $resource;
                break;
        }
    }
    
    header('Content-Type: application/json');
    $dataplan = json_encode($customers);
    $objs = json_decode($dataplan);
    echo $objs;
    

    Assuming that the file where the code above is called api-plans.php, in the front file, I used the following code

    <body>
        <div class="content"></div>
    </body>
    <script type="text/javascript" src="path_your_js/jquery.min.js"></script>
    <script>
        $(document).ready(function(){
            $.ajax({
                type: "post",
                url: "api-plans.php",
                async: true,
                data: "",
                beforeSend: function() {
                },
                success: function( response ) {
                    responseJson = JSON.parse(response);
                    var contentHTML = "";
                    var responseJsonNetWork = responseJson[0]['BUNDLE'];
    
                    $.each(responseJsonNetWork, function(i, item) {
                        contentHTML = contentHTML + "Data Plan: " + responseJsonNetWork[i]['dataPlan'] + "<br />";
                        contentHTML = contentHTML + "Duration: " + responseJsonNetWork[i]['duration'] + "<br />";
                        contentHTML = contentHTML + "Type: " + responseJsonNetWork[i]['type'] + "<br />";
                        contentHTML = contentHTML + "Status: " + responseJsonNetWork[i]['status'] + "<br />";
                        contentHTML = contentHTML + "Price (basic_user): " + responseJsonNetWork[i]['price'][0]['basic_user'] + "<br /><br />";
                    });
    
                    $(".content").html(contentHTML);
                }
            });
        });
    </script>
    

    Hope this helps

    Login or Signup to reply.
  2. $url = "https://subandgain.com/api/databundles.php";
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($curl);
    curl_close($curl);
    $obj = json_decode($response, true);
    $suppliers = array();
    $customers = array();
    $temp = array();
    $dataPlan = array();
    $kPrice = array();
    
    foreach ($obj as $rkey => $resource) {
    
        $allPlans = array();
        foreach ($resource['BUNDLE'] as $key) {
            $dataPlan = array('dataPlan'=>$key['dataPlan'], 'duration'=>$key['duration'], 'type'=>$key['type'], 'status'=>$key['status']);
            $kPrice = array();
    
            foreach ($key['price'] as $keyPrice){
                $kPrice = array('price_basic_user' => $keyPrice['basic_user']);
    
    //            $dataPlan['Price_basic_user'] = $keyPrice['basic_user']; // if you want [Price_basic_user] => 43 below [status] => Active
    
    //            $kPrice[] = array('price_basic_user' => $keyPrice['basic_user']); // if there are multiple basic_user price uncomment
            }
    
            $dataPlan['Price'] = $kPrice; // comment this if you want [Price_basic_user] => 43 below [status] => Active
            $allPlans[] = $dataPlan;
        }
        $temp[$resource['NETWORK']] =  $allPlans;
    
    }
    echo '<pre>';
    print_r($temp);
    

    Try this!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search