0
        $url="https://maps.googleapis.com/maps/api/geocode/xml? address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false&key=..."
        $ch = curl_init($url);
        $response = curl_exec($ch);
        curl_close($ch);
    $response = new SimpleXMLElement($response);
        $latitude = $response->result->geometry->location->lat;
        echo $latitude;

This Returns Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML'

I have tried adding oe=utf-8 to the url but not improving, when I load the website in the browser I can see the result fine althought with the header "This XML file does not appear to have any style information associated with it. The document tree is shown below." How can I reformat this so SimpleXMLElement can read it?

Update: file_get_contents($url); works fine I've tried and with that SimpleXMLElement() can be used, I have also tried adding different curl_setopts but still they dont return the XML file. I am still trying to do it with curl if anyone can help me with this.

curl_setopt($ch, CURLOPT_FAILONERROR,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);

Thanks

  • You haven't set `CURLOPT_RETURNTRANSFER` that I see here. That means your `$response` is a boolean, not the XML string. `var_dump($response)` to see what you actually have. – Michael Berkowski Mar 13 '14 at 01:28
  • hint: the URL lacks the *userip* parameter; die($response); might provide some insight what's wrong. – Martin Zeitler Mar 13 '14 at 01:35
  • Thanks, Michael: Mummmm I am not sure I understand but as you say when I do the dump returns Bool(false), I had added curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); before but that doesnt look that makes any difference, still the output of the curl request is that boolean rather than the XML file that opens if I load the url in the browser . – user3413269 Mar 13 '14 at 02:28

0 Answers0