Free Source Code - Codeintra

Documentation To Setup IP API in PHP/JAVA - KrishnaIP

Free Source Code - Codeintra
IP API Integration Guide

Welcome to the API Integration Guide! This document will help you seamlessly integrate our API into your website using PHP / JAVA. Our API provides detailed information about IP addresses, including geographical location, time zone, currency, and more.

Step 1: Obtaining API Key To authenticate your requests and ensure security, you'll need to obtain an API key from our platform. Please follow these steps to generate your unique API key:

  • Visit our website at https://krishnaip.net
  • Sign up for an account or log in if you already have one. 
  • Navigate to the API section in your account settings. 
  • Generate an API key and copy it for future use.

Step 2: Making API Requests Now that you have your API key, you can start making API requests. Here's an example of how to make a request:

1. Open your app's build.gradle file and add the following dependencies:

dependencies {
    // OkHttp for making HTTP requests
    implementation 'com.squareup.okhttp3:okhttp:4.9.1'

    // Gson for JSON parsing
    implementation 'com.google.code.gson:gson:2.8.8'
}

2. Now, you can use the following code to make the API request and parse the JSON response using OkHttp and Gson:

import com.google.gson.Gson;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.IOException;

public class MyApiManager {
    // Define your API endpoint URL
    private static final String API_URL = "https://krishnaip.net/api/ipinfo/IP_Address/Api_Key";

    // Create an OkHttpClient instance
    private OkHttpClient client;

    // Gson instance for JSON parsing
    private Gson gson;

    public MyApiManager() {
        client = new OkHttpClient();
        gson = new Gson();
    }

    public void fetchGeolocationData(String ipAddress, String apiKey, final GeolocationCallback callback) {
        // Build the full API URL with the provided IP address and API key
        String url = API_URL.replace("IP_Address", ipAddress).replace("Api_Key", apiKey);

        // Create the HTTP request
        Request request = new Request.Builder()
                .url(url)
                .build();

        // Make the asynchronous request
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
                // Handle request failure
                callback.onFailure(e.getMessage());
            }

            @Override
            public void onResponse(Response response) throws IOException {
                if (response.isSuccessful()) {
                    // Parse the JSON response into a GeolocationData object
                    String jsonResponse = response.body().string();
                    GeolocationData data = gson.fromJson(jsonResponse, GeolocationData.class);

                    // Pass the geolocation data to the callback
                    callback.onSuccess(data);
                } else {
                    // Handle non-successful response
                    callback.onFailure("API request failed with status code: " + response.code());
                }
            }
        });
    }
}

3. Create a new Java class file, let's say `GeolocationCallback.java`, and place the `GeolocationCallback` interface inside it.

public interface GeolocationCallback {
    void onSuccess(GeolocationData data);
    void onFailure(String error);
}

4. Now create a GeolocationData model class and replace its content with the content of the below code.

public class GeolocationData {
    private String status;
    private String message;
    private Krishnaip krishnaip;

    public GeolocationData(String status, String message, Krishnaip krishnaip) {
        this.status = status;
        this.message = message;
        this.krishnaip = krishnaip;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Krishnaip getKrishnaip() {
        return krishnaip;
    }

    public void setKrishnaip(Krishnaip krishnaip) {
        this.krishnaip = krishnaip;
    }

    public static class Krishnaip {
        private String continent;
        private String country;
        private String city;
        private String capital;
        private String country_code;
        private String country_alpha_3_code;
        private String region;
        private String postal;
        private double latitude;
        private double longitude;
        private String network;
        private String timeZone;
        private String organization;

        public Krishnaip() {
        }
        
        // Getters and setters for the fields

        public String getContinent() {
            return continent;
        }

        public void setContinent(String continent) {
            this.continent = continent;
        }

        public String getCountry() {
            return country;
        }

        public void setCountry(String country) {
            this.country = country;
        }

        public String getCity() {
            return city;
        }

        public void setCity(String city) {
            this.city = city;
        }

        public String getCapital() {
            return capital;
        }

        public void setCapital(String capital) {
            this.capital = capital;
        }

        public String getCountry_code() {
            return country_code;
        }

        public void setCountry_code(String country_code) {
            this.country_code = country_code;
        }

        public String getCountry_alpha_3_code() {
            return country_alpha_3_code;
        }

        public void setCountry_alpha_3_code(String country_alpha_3_code) {
            this.country_alpha_3_code = country_alpha_3_code;
        }

        public String getRegion() {
            return region;
        }

        public void setRegion(String region) {
            this.region = region;
        }

        public String getPostal() {
            return postal;
        }

        public void setPostal(String postal) {
            this.postal = postal;
        }

        public double getLatitude() {
            return latitude;
        }

        public void setLatitude(double latitude) {
            this.latitude = latitude;
        }

        public double getLongitude() {
            return longitude;
        }

        public void setLongitude(double longitude) {
            this.longitude = longitude;
        }

        public String getNetwork() {
            return network;
        }

        public void setNetwork(String network) {
            this.network = network;
        }

        public String getTimeZone() {
            return timeZone;
        }

        public void setTimeZone(String timeZone) {
            this.timeZone = timeZone;
        }

        public String getOrganization() {
            return organization;
        }

        public void setOrganization(String organization) {
            this.organization = organization;
        }
        
        // Additional getters and setters for other fields based on your JSON structure
    }
}

To display data in a `TextView`, you can follow these steps in your Android application:

5. In your XML layout file, add a `TextView` widget. Give it an `id` to reference it in the Java code. For example:

<TextView
    android:id="@+id/dataTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="16sp"
    android:textColor="@android:color/black" />

6. In your Java code (e.g., an `Activity` or `Fragment`), find the `TextView` by its `id` using `findViewById()`. For example:

TextView dataTextView = findViewById(R.id.dataTextView);

For example, to display the `city` field from your `GeolocationData` object:,

dataTextView.setText(data.getKrishnaip().getCity());

Step 3: Error Handling It's essential to handle any errors that may occur during API requests. Here's an example of how to handle errors using PHP:

The API response contains a 'status' field that indicates the result of the API request. Here are the possible status values and their meanings:

 

'success': This status indicates that the API request was successful, and the desired information can be extracted from the response.

 

'Invalid IP address': If the provided IP address in the API request is invalid or not recognized, this status will be returned. Make sure to verify that the IP address is valid before making the request.

 

'Failed to retrieve geolocation data': This status indicates that the API was unable to retrieve the geolocation data for the given IP address. It might occur if the IP address is not accessible or if there are other technical issues. If you encounter this status, please try again later or contact our support team for further assistance.

 

'Invalid API Key': If the API key provided in the request is invalid, this status will be returned. Make sure you are using a valid and active API key obtained from our platform.

 

Here's an example of how you can handle these status responses in your code:

String status = data.getStatus();
if (status.equals("ip_error")) {
    callback.onFailure("IP Error: "   data.getMessage());
} else if (status.equals("data_error")) {
    callback.onFailure("Data Error: "   data.getMessage());
} else if (status.equals("api_error")) {
    callback.onFailure("API Error: "   data.getMessage());
} else {
    callback.onSuccess(data.getGeolocationData()); //data.getKrishnaip().getCity();
}
API Parameters

API URL: The API URL is the endpoint you will be sending your requests to.

'IP_Address' (required): The IP address you want to retrieve information for. The API will provide details related to this IP address.

'Api_Key' (required): Your unique API key obtained from our platform. This key is necessary for authenticating your requests and ensuring the security of your integration.

Example API Request: An API request URL might look like this:

https://krishnaip.net/api/ipinfo/123.45.67.89/your-api-key-here
API Success Response

JSON Response Structure: The API response is formatted in JSON and contains various fields of information related to the provided IP address. Here is an example of the JSON response structure:

{
    "status": "success",
    "message": "Successfully found geolocation data",
    "krishnaip": {
        "continent": "Europe",
        "country": "Czechia",
        "city": "Opava",
        "capital": "Prague",
        "country_code": "CZ",
        "country_alpha_3_code": "CZE",
        "region": "Opava District",
        "postal": "746 01",
        "latitude": 49.9401,
        "longitude": 17.6363,
        "network": "213.155.232.0/22",
        "timeZone": "Europe/Prague",
        "organization": "Nej.cz s.r.o.",
        "system_number": 31246,
        "telephone_code": "420",
        "currency_code": "CZK",
        "currency_name": "Czech Koruna",
        "currency_symbol": "Kč",
        "lang_code": "CS",
        "lang_name": "Czech",
        "country_domain_name": "cz",
        "flag_w40": "http://192.168.1.5:8080/assets/flags/png/w40/cz.png",
        "flag_w80": "http://192.168.1.5:8080/assets/flags/png/w80/cz.png",
        "flag_w160": "http://192.168.1.5:8080/assets/flags/png/w160/cz.png",
        "flag_w320": "http://192.168.1.5:8080/assets/flags/png/w320/cz.png",
        "flag_w640": "http://192.168.1.5:8080/assets/flags/png/w640/cz.png",
        "flag_svg": "http://192.168.1.5:8080/assets/flags/svg/cz.svg"
    }
}
Invalid API Key Response
{"status":"api_error","message":"Invalid API Key","code":400}
Invalid IP address Response
{"status":"ip_error","message":"Invalid IP address","code":400}
No Data Response
{"status":"data_error","message":"Failed to retrieve geolocation data","code":500}
Response Parameters Description:

- `status`: Indicates the status of the API response. Possible values are: "success" for a successful response or potentially error statuses for different error scenarios.

- `message`: A message indicating the success or reason for any failures in finding geolocation data.

- `krishnaip`: Contains geolocation data for the provided IP address.

- `continent`: The continent where the IP address is located.

- `country`: The country where the IP address is located.

- `city`: The city where the IP address is located.

- `capital`: The capital city of the country.

- `country_code`: The ISO 3166-1 country code for the country.

- `country_alpha_3_code`: The ISO 3166-1 alpha-3 country code for the country.

- `region`: The region or state where the city is located.

- `postal`: The postal code or zip code for the area.

- `latitude`: The latitude coordinates of the location.

- `longitude`: The longitude coordinates of the location.

- `network`: The IP network associated with the IP address.

- `timeZone`: The time zone of the location.

- `organization`: The organization or ISP associated with the IP address.

- `system_number`: A numeric identifier.

- `telephone_code`: The international telephone code for the country.

- `currency_code`: The ISO 4217 currency code for the country.

- `currency_name`: The name of the currency used in the country.

- `currency_symbol`: The currency symbol used in the country.

- `lang_code`: The ISO language code for the primary language spoken in the country.

- `lang_name`: The name of the primary language spoken in the country.

- `country_domain_name`: The top-level domain name associated with the country.

- `flag_w40`: The URL for the flag image with a width of 40 pixels.

- `flag_w80`: The URL for the flag image with a width of 80 pixels.

- `flag_w160`: The URL for the flag image with a width of 160 pixels.

- `flag_w320`: The URL for the flag image with a width of 320 pixels.

- `flag_w640`: The URL for the flag image with a width of 640 pixels.

- `flag_svg`: The URL for the flag SVG image.

Support: If you have any questions or encounter any issues while integrating our API, please don't hesitate to contact our support team. Get in touch via Live Support Chat