2

Below api(s) are perfectly working, i am getting proper output from "plan-api.php"

PHP CODE(plan-api.php)

if ($_GET['result']):
$rslt = $_GET['result'];
$result = Unirest\Request::get("https://sphirelabs-mobile-number-portability-india-operator-v1.p.mashape.com/index.php?number=$rslt",
array(
"X-Mashape-Key" => "XXXXXXXXXX",
"Accept" => "application/json"
)
);

$optid= rawurlencode($result->body->Operator);
$cirid=rawurlencode($result->body->{'Telecom circle'});

endif;  


// Create a stream
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"X-Mashape-Key: XXXXXXXXX"               
 )
 );

$context = stream_context_create($opts);

// Open the file using the HTTP headers set above
$requestUrl = "https://tariff-plan-api-datayuge.p.mashape.com/index.php?circleid=$cirid&limit=50&operatorid=$optid&recharge_type=3g";
$response = (file_get_contents($requestUrl, false, $context));

$data = json_decode($response, true);

$data2= json_encode($data);

echo $data2;

OUTPUT (from plan-api.php)

{"data":[{"id":"4401","operatorid":"Reliance GSM","circleid":"Bihar Jharkhand","recharge_amount":"60","recharge_talktime":"60","recharge_validity":"NA ","recharge_shortdesc":"Recharge of Rs 60 By Reliance GSM","recharge_longdesc":"Full Talktime ","recharge_type":"Full Talktime"},{"id":"4407","operatorid":"Reliance GSM","circleid":"Bihar Jharkhand","recharge_amount":"110","recharge_talktime":"110","recharge_validity":"Lifetime Validity","recharge_shortdesc":"Recharge of Rs 110 By Reliance GSM","recharge_longdesc":"Full Talktime ","recharge_type":"Full Talktime"},{"id":"4409","operatorid":"Reliance GSM","circleid":"Bihar Jharkhand","recharge_amount":"150","recharge_talktime":"150","recharge_validity":"Lifetime Validity","recharge_shortdesc":"Recharge of Rs 150 By Reliance GSM","recharge_longdesc":"Full Talktime ","recharge_type":"Full Talktime"},{"id":"4414","operatorid":"Reliance GSM","circleid":"Bihar Jharkhand","recharge_amount":"555","recharge_talktime":"600","recharge_validity":"Lifetime Validity","recharge_shortdesc":"Recharge of Rs 555 By Reliance GSM","recharge_longdesc":"Extra Talktime","recharge_type":"Full Talktime"}]}

HTML/AJAX(plan-ajax.php)

Here i am getting output in textbox if i put the value of circleid and operatorid but when i am trying to put the php objects output of first api $optid$cirid; than i am not getting output in below textboxes.

I tried to add $('#opt'+key).val(value.operator); with textbox id "opt" but same no output

Please enter a Mobile number
<input type="text" id="search">
<br>


<input type="text" id="result0">
<input type="text" id="result1">
<input type="text" id="result2">
<input type="text" id="result3">
<br>
<input type="text" id="talk0">
<input type="text" id="talk1"> 
<input type="text" id="talk2">
<input type="text" id="talk3">


<script>
$(document).ready(function() {
$('#search').keypress(function(){
    $.ajax({

        type: "GET",
        url: "plan-api.php",
        data: 'result=' + $('#search').val(),
        dataType: "json",

        success: function(responseText){

            $.each(responseText.data, function(key,value){

    $('#result'+key).val(value.recharge_amount);
       $('#talk'+key).val(value.recharge_talktime);

            });
        }

    }); // Ajax Call
}); //event handler

}); //document.ready
</script>
1
  • var_dump of $response from api would be helpful Commented Jul 19, 2015 at 5:28

1 Answer 1

2

Output from API is incorrect, it must be as following. Basically, it's not in JSON format.

{"data":[{"id":"4401","operatorid":"Reliance GSM","circleid":"Bihar   Jharkhand","recharge_amount":"60","recharge_talktime":"60","recharge_validity":"NA ","recharge_shortdesc":"Recharge of Rs 60 By Reliance GSM","recharge_longdesc":"Full Talktime ","recharge_type":"Full Talktime"},{"id":"4407","operatorid":"Reliance GSM","circleid":"Bihar Jharkhand","recharge_amount":"110","recharge_talktime":"110","recharge_validity":"Lifetime Validity","recharge_shortdesc":"Recharge of Rs 110 By Reliance GSM","recharge_longdesc":"Full Talktime ","recharge_type":"Full Talktime"},{"id":"4409","operatorid":"Reliance GSM","circleid":"Bihar Jharkhand","recharge_amount":"150","recharge_talktime":"150","recharge_validity":"Lifetime Validity","recharge_shortdesc":"Recharge of Rs 150 By Reliance GSM","recharge_longdesc":"Full Talktime ","recharge_type":"Full Talktime"},{"id":"4414","operatorid":"Reliance GSM","circleid":"Bihar Jharkhand","recharge_amount":"555","recharge_talktime":"600","recharge_validity":"Lifetime Validity","recharge_shortdesc":"Recharge of Rs 555 By Reliance GSM","recharge_longdesc":"Extra Talktime","recharge_type":"Full Talktime"}]}

in success: of ajax you can put console.log(responseText) for clarity.

Remove the following from plan-api.php:

echo $optid;
echo $cirid;
Sign up to request clarification or add additional context in comments.

2 Comments

Reliance%20GSMBihar%20Jharkhand this is the output from first api
@user3588059 Okay. Then remove echo $optid; echo $cirid; from plan-api.php. I am assuming that is your actual code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.