下面是API:https://pincode.saratchandra.in/api/pincode/{any pin code}
例子:我有https://pincode.saratchandra.in/api/pincode/500022
上面的API请求返回以下数据:
{“地位”:200,“数据”:{“taluk”:“Khairatabad”,"state_name":"TELANGANA",“region_name”:“海得拉巴”,“region_name”:"pincode":"500022",“office_name”:“中央秘书处S.O","id":4686,”division_name“:”海得拉巴“,”地区“:”海得拉巴“,”delivery_status“:”交付“,"circle_name":"Andhra”}
我想在我的网页上显示它如下
塔鲁克:海拉塔巴德州名称: Telengana
诸若此类
我想要一个表单,该表单接收用户的pincode,并使用特定的pincode发出请求,并获取该pincode的数据并将其显示在站点上。
我一直在努力奋斗,但我找不到实现它的方法。
发布于 2016-01-23 07:32:00
使用Ajax或jQuery.getJSON()访问它
$.getJSON( "https://pincode.saratchandra.in/api/pincode/500022", function( data ) {
/* data is the fetch result from the api
* you can access taluk field by data.data.taluk
* or state name by data.data.state_name */
console.log(data);
});但是,如果您的网站是托管在其他地方,您将有一个CORS (跨源资源共享)问题。
除非pincode.saratchandra.in服务器被配置为允许CORS,否则使用这种方法的浏览器无法做到这一点。您的服务器可以为您发出请求,也可以与pincode.sratchandra.in联系以允许您的域/服务器访问数据。
您还可以使用php或其他服务器端脚本来访问数据。例如,在php中,除非服务器(pincode.sratchandra.in)允许您这样做,否则。
下面是php中的一个示例:
<?php
$json = file_get_contents('https://pincode.saratchandra.in/api/pincode/500022');
$obj = json_decode($json);
echo 'Taluk: ' + $obj->data->taluk;
echo '<br>';
echo 'State Name: ' + $obj->data->state_name;发布于 2017-08-23 13:00:10
你好。 我知道我是在回答这个问题,这个问题对提问的用户来说不再重要。但是对于那些同样面临同样问题的人来说,可以从这个例子中得到帮助。 这就是为什么我要回答这个问题。 若要在代码下面运行,请确保系统中的curl必须启用。否则就行不通了。
<?php
if(isset($_POST['pincode']))
{
$pincode = $_POST['pincode'];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => "https://pincode.saratchandra.in/api/pincode/" . $pincode
));
$resp = curl_exec($curl);
$temp = json_decode($resp);
if($temp->status ==200)
{
$records = $temp->data;
$str ="";
foreach ($records as $key => $values)
{
$str.= "pincode :" .$values->pincode."</br>";
$str.= "office_name :".$values->office_name."</br>";
$str.= "delivery_status :" .$values->delivery_status."</br>";
$str.= "division_name :".$values->division_name."</br>";
$str.= "region_name :" .$values->region_name."</br>";
$str.= "circle_name :".$values->circle_name."</br>";
$str.= "district :".$values->district."</br>";
$str.= "state_name :" .$values->state_name."</br>";
$str.= "taluk :".$values->taluk."</br><hr>";
}
echo $str;
curl_close($curl);
}
else
{
echo $temp->message;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>User Form</title>
</head>
<body>
<form name="user_form" method="post" action="" id="user_form">
<input type="pincode" name="pincode" value="" placeholder="Enter pincode" required="">
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>如果任何一个人有问题,那么可以自由地问。
https://stackoverflow.com/questions/34960481
复制相似问题