简体   繁体   中英

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

Here is the API: https://pincode.saratchandra.in/api/pincode/{any pin code}

Example: I have https://pincode.saratchandra.in/api/pincode/500022

The above API request returns the following data:

{"status":200,"data":[{"taluk":"Khairatabad","state_name":"TELANGANA","region_name":"Hyderabad City","pincode":"500022","office_name":"Central Secretariat SO","id":4686,"division_name":"Hyderabad City","district":"Hyderabad","delivery_status":"Delivery","circle_name":"Andhra Pradesh"}]}

I want to display it in my webpage as follows

Taluk : Khairatabad
State name: Telengana

and so on

I want a form that receives the pincode from the user and makes the request with the particular pincode and gets the data for that pincode and displays it on the site.

I have been struggling so hard but I couldn't find a way to implement it.

You access it using Ajax or 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);
});

But if your website is hosted else where you will have a CORS (Cross Origin Resource Sharing) issues.

You can't do this with a browser with this approach unless the pincode.saratchandra.in server is configured to allow CORS . Your server could make the request for you or you can contact pincode.sratchandra.in to allow your domain/server to access the data.

You can also use php or other server side script to access the data. for example in php unless the server (pincode.sratchandra.in) allows you to do so.

Here's an example 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;

Hello.
i know i am replying for this question which is no longer important for user who asked question. but for those who are also facing same issues can get help from this example.
that's why i am answering this question.
to run below code make sure in your system curl must be enable. otherwise it will not worked.

      <?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>

if any one having issues then can freely ask.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM