2

My php file:

<?php

 if (isset($_POST['query'])) {  
    $query = $_POST['query'];

$url='http://maps.googleapis.com/maps/api/geocode/json?address=';
$callback="&callback=?";
$sensor='&sensor=false';
$result= $url.$query.$sensor.$callback;
curl_init ($result);

 }
?>

My html file:

<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){


    // when the user clicks the button
    $("button").click(function(){


          $.getJSON("geo.php",function(json){

             $('#results').append('<p>Latitude : ' + json.results[9].geometry.location.lat+ '</p>');
             $('#results').append('<p>Longitude: ' + json.results[9].geometry.location.lng+ '</p>');

});

          // get the json file
    });
});


</script>

</head>
<body>

<input type="text" id="query" /><button>Get Coordinates</button>
<div id="results"></div>

</body>
</html>

I get this error when i used firbug:

json is null
$('#results').append('<p>...s[9].geometry.location.lat+ '</p>'); 

Google's json response:

{
  "status": "OK",
  "results": [ {
    "types": [ "street_address" ],
    "formatted_address": "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
    "address_components": [ {
      "long_name": "1600",
      "short_name": "1600",
      "types": [ "street_number" ]
    }, {
      "long_name": "Amphitheatre Pkwy",
      "short_name": "Amphitheatre Pkwy",
      "types": [ "route" ]
    }, {
      "long_name": "Mountain View",
      "short_name": "Mountain View",
      "types": [ "locality", "political" ]
    }, {
      "long_name": "California",
      "short_name": "CA",
      "types": [ "administrative_area_level_1", "political" ]
    }, {
      "long_name": "United States",
      "short_name": "US",
      "types": [ "country", "political" ]
    }, {
      "long_name": "94043",
      "short_name": "94043",
      "types": [ "postal_code" ]
    } ],
    "geometry": {
      "location": {
        "lat": 37.4219720,
        "lng": -122.0841430
      },
      "location_type": "ROOFTOP",
      "viewport": {
        "southwest": {
          "lat": 37.4188244,
          "lng": -122.0872906
        },
        "northeast": {
          "lat": 37.4251196,
          "lng": -122.0809954
        }
      }
    }
  } ]
}

EDITED:

<?php
if (isset($_POST['query'])) {  
    $query = $_POST['query'];
$url='http://maps.googleapis.com/maps/api/geocode/json?address=';
$callback="&callback=?";
$sensor='&sensor=false';
$result= $url.$query.$sensor.$callback;
$resp = file_get_contents($result);
                header('Cache-Control: no-cache, must-revalidate');
                header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
                header('Content-type: application/json');
echo $resp;
}

2 Answers 2

2

You're not outputting anything from the PHP. The PHP script has to output JSON for getJSON to be able to receive it.

Have a look at curl_init. I think you want to be doing this:

$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $result);
curl_setopt($ch, CURLOPT_HEADER, 0);

// grab URL and pass it to the browser
curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks.I was expecting curl will output directly.
Try visiting the URL of your PHP script directly and debug from there.
You need to debug the PHP - the issue isn't in the JavaScript. Trying putting some echo statements in there to see if everything you thought was working is actually working. Sorry, can't help any more than that!
I have tried echoing $url,$sensor,$callback ...all get is a blank page.
Ok, change ($_POST['query']) to ($_REQUEST['query']) and pass the query in as a query string - e.g. "geo.php?New+York". The problem is that everything is wrapped in a check for a post parameter which isn't being sent when you do a GET.
1

Are you sure you are capturing the result?

I'm assuming this: Your PHP code is called via Ajax from the html. So basically you are constructing the URL for the query and using CURL to fetch the json result. It seems you are not actually capturing or returning the results to the ajax call You might want to do this:


            $process = curl_init($url); 
//init curl connection
            curl_setopt($process, CURLOPT_HEADER, 0); 

              curl_setopt($process, CURLOPT_POST, 1); 

            curl_setopt($process, CURLOPT_RETURNTRANSFER,1);

            curl_setopt($process,CURLOPT_CONNECTTIMEOUT,1);

            $resp = curl_exec($process); 
//your content
            curl_close($process); 
//$resp contains your response
                header('Cache-Control: no-cache, must-revalidate');
                header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
                header('Content-type: application/json');
echo $resp;

35 Comments

Ok Try calling the script directly and use error_log to help you debug this. I find that debugging with error_log and print_r helps me see where in te code there is an error.
error_log(print_r($vartocheck,true),3,"destination of log file"); also don't forget to check the apache log file for the errors, maybe you are simply overlooking another error.
Can u tell me what is destination log file? Do i have to create it or it already present. so that i just have to point to it
either is valid. You can create it and assign rights so that apache can write to it. or you can use an existing file. touch logfile.txt; chmod 777 logfile.txt then just set the complete path to it in script.
and if you have ssh access, you can actually tail -f the log file and you 'll get real time log info.
|

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.