1

I'm working with the google places api and I currently have a json of a list of restaurants and details of them near the users location. In my code I take this json and put it in a php array and in my javascript I'm trying to access each element inside of this array. I have the following code which should work but for some reason it isn't and I can't figure out why. I've searched other questions on this topic and none of them have helped me.

define("PLACES_API_ENDPOINT", "https://maps.googleapis.com/maps/api/place/textsearch/json?query=restaraunts+near".$address);

$curl1 = curl_init();

curl_setopt($curl1, CURLOPT_URL, PLACES_API_ENDPOINT . "&key=mykey");

curl_setopt($curl1, CURLOPT_RETURNTRANSFER, true);

curl_setopt($curl1, CURLOPT_SSL_VERIFYPEER, false);

$response = curl_exec($curl1);

/*echo "<hr>";*/
//var_dump($response);

$response = json_decode($response, true);

<script>
  window.onload=function(){
      console.log("<?php echo $response['results'] ?>");
    };
</script>    
    
  • 1
    Possible duplicate of [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – Greg Schmidt Apr 26 '19 at 23:13
  • Take a look at your rendered output you more than likely encountering a syntax error. As what you echo out is going to likely have quotes `"` in it. And thus the js interpertor is going to see a prematurely ended expression. E.g. `console.log("["text"]")` – Patrick Evans Apr 26 '19 at 23:30
  • I do console.log(""); I a syntax error with console.log('"string(15) "Marina's Cocina""'); I just want the name "Marina's Cocina" Not the "string(15)". – Kyle Van Housen Apr 27 '19 at 00:20
  • That's because you are not using `echo` with the response and doing a `var_dump` instead. – Second2None Apr 27 '19 at 00:36
  • 1
    console.log(""); – Second2None Apr 27 '19 at 00:36

0 Answers0