下面的api(s)是完美的工作,我得到适当的输出从"plan-api.php"
PHP代码(plan-api.php)
if ($_GET['result']):
$rslt = $_GET['result'];
$result = UnirestRequest::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)
这里我得到输出在文本框中,如果我把circleid
和operatorid
的值,但当我试图把php对象输出的第一个api $optid
$cirid;比我没有得到输出在下面的文本框。
我试图添加$('#opt'+key).val(value.operator);
与文本框id"opt",但同样没有输出
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>
API的输出不正确,必须如下所示。基本上,它不是JSON格式。
{"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"}]}
在ajax的success:
,你可以把console.log(responseText)
为清晰。
从plan-api.php
中删除以下内容:
echo $optid;
echo $cirid;