我有一个API,我想从api响应中获取数据

I have an API, I want to get the data from api response

本文关键字:响应 获取 数据 api 有一个 API      更新时间:2023-09-26

这是API:https://pincode.saratchandra.in/api/pincode/{any pin code}

示例:我有 https://pincode.saratchandra.in/api/pincode/500022

上述 API 请求返回以下数据:

{"status":

200,"data":[{"taluk":"Khairatabad","state_name":"TELANGANA","region_name":"Hyderabad 城市","密码":"500022","office_name":"中央秘书处 S.O","id":4686,"division_name":"海得拉巴 城市","区":"海得拉巴","delivery_status":"交货","circle_name":"安得拉 Pradesh"}]}

我想在我的网页中显示它,如下所示

塔鲁克 : 海拉塔巴德
州名: 特伦加纳

等等

我想要一个表单,该表单从用户那里接收密码并使用特定密码发出请求,并获取该密码的数据并将其显示在网站上。

我一直在努力奋斗,但我找不到实施它的方法。

您可以使用 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;

你好。
我知道我正在回答这个问题,这个问题不再是 对于提出问题的用户很重要。但对于那些也是 面对同样的问题可以从此示例中获得帮助。
这就是我回答这个问题的原因。
要运行以下代码,请确保在系统中必须启用 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>

如果有人有问题,那么可以自由询问。