0

I have an AJAX request file that looks like this:

$.ajax({
        url: 'libs/php/getCountryBorder.php',
        type: 'POST',
        dataType: 'json',
        data: {
            name: 'Bahamas',
        },
        success: function (response) {
            console.log(response);
        },
        error: function (jqXHR, textStatus, errorThrown) {
            console.log(errorThrown);
        },
    });

I also have a geojson file that looks like this:

You can view my local geojson file here Geojson File

enter image description here

Here is my PHP routine call to the local file geo.json file:

  ini_set('display_errors', 'On');
  error_reporting(E_ALL);

  $url = 'https://stephenorowole.co.uk/vendors/countryBorder/countryBorders.geo.json?&name=' . $_REQUEST['name'];

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_URL, $url);

  $response = curl_exec($ch);
  curl_close($ch);
  $jsonResponse = json_decode($response, true);

  $output['data'] = $jsonResponse['features'];
  header('Content-Type: application/json; charset=UTF-8');
  echo json_encode($output);

?>

So, I made an Ajax call from another file to the php to get only a specific name in the json file. It doesn't return only the name data but all the data in the json file. What I want is a specific data in the json to be returned through the request. I don't know how to do this with local file. It's easy for me to do if I'm working with external API.

In external API, if a request is made to return only the country that matches a specific latitude and longitude, the specific data will be returned for that specific request. However, I don't know how to achieve this when I'm working with a local file.

I want to be able to get geometry of a country based on the name of the country given as a request data when I make the call. I've just added the ajax call.

Any help will be much appreciated.

3
  • You're going to have to do the data extraction yourself. i.e. loop through the records to find the one you want. Commented Jun 17, 2021 at 16:22
  • Ok, assuming I want to have an ajax get the list of names separate and another ajax request to get the geometry specific to that name, do I need to create different php files for each request. Commented Jun 17, 2021 at 16:32
  • Please add your code to the question as text rather than images of your code Commented Jun 17, 2021 at 16:46

2 Answers 2

0

If you only have a local (complete) json file locally which serves as a local DB, you will have to manually build your result json.

I am not sure I grasped what you were trying to achieve, would you be able to share your calls?

Is there a reason why you don't call the API directly in your Ajax call? It doesn't seem you are doing much in your PHP call...

Sign up to request clarification or add additional context in comments.

6 Comments

I want to be able to get geometry of a country based on the name of the country given as a request data when I make the call. I've just added the ajax call
Added a comment here. Why don't you call the API directly in your ajax call? You don't seem to do much in your php and it would simplify the logic. As I said, if you do not want to call the API multiple time but leverage a json file you have stored on your server, you have no choice but to loop over it and build a response manually, which I would discourage.
I'm just following a guidline from my tutor. Even though I make the call directly, I still need to find a way make a request based on a specific name e.g, Canada
It's not clear at.this point how we can help you. What are you seeing when you run the code?
As I mentioned in the question, it return the whole json data
|
0

Apparently, making a request to local file with a set parameter can't work since the is no backend code writing to handle an http get resquest for the specific data. I can only get a specific data from a local json file by adding to my php code above.

Here is my new complete php code that get the specific data from the geojson file:

<?php
  ini_set('display_errors', 'On');
  error_reporting(E_ALL);

  $url = 'https://stephenorowole.co.uk/vendors/countryBorder/countryBorders.geo.json';

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_URL, $url);

  $response = curl_exec($ch);
  curl_close($ch);
  $jsonResponse = json_decode($response, true);


 $output = null;
  foreach($jsonResponse['features'] as $data){
    if($data['properties']['name'] == $_REQUEST['name']){
      $output['data'] = $data;
      break; 
    };
  };

  header('Content-Type: application/json; charset=UTF-8');
  echo json_encode($output);

?>

This is the code snipet in the php file that did the trick:

  foreach($jsonResponse['features'] as $data){
    if($data['properties']['name'] == $_REQUEST['name']){
      $output['data'] = $data;
      break; 
    };
  };```

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.