0

I am getting JSON data through an API using jQuery. Now I am unable to get the JSON data in my PHP code

	<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

<script type="application/javascript">
	$(document).ready(function() {
	$.getJSON("http://www.telize.com/geoip?callback=?",
		function(json) {
    		document.write("Geolocation information for IP address : ", json.ip);
    		document.write("Country  : ", json.country);
    		document.write("Latitude : ", json.latitude);
	    	document.write("Longitude : ", json.longitude);
		}
	);
});
</script>

//result is below on browser
//Geolocation information for IP address : 45.114.126.32Country : PakistanLatitude : 30Longitude : 70

Now I want to get the latitude and longitude values in my PHP code. How can I parse this code?

1 Answer 1

1

I suggest you send the json data via ajax:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

<script type="application/javascript">
    $(document).ready(function() {
    $.getJSON("http://www.telize.com/geoip?callback=?",
        function(json) {

            $.ajax({
                   type: 'post',
                   url: 'action_receive_json.php';
                   data : json
                   });
        }
    );
});
</script>

Then you can manipulate back the JSON data in PHP code (in this case action_receive_json.php file):

$country = $_POST['country'];
$latitude = $_POST['latitude'];
$longitude = $_POST['longitude'];

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

1 Comment

Oh..Php code? Create a php file anywhere you please... :) as long as the url same as in ajax.

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.