0

I am working on a little project for a programming course here on my university. It involves getting data from the google api (JSON) and providing some of that information to the user.

function compare($city, $start, $destination)
{
    // merge city with start and destination
    $city_start = $start . ', ' . $city;
    $city_destination = $destination . ', ' . $city;

    // reject symbols that start with ^
    if (preg_match("/^\^/", $city) OR preg_match("/^\^/", $start) OR preg_match("/^\^/", $destination))
    {
        return false;
    }

    // reject symbols that contain commas
    if (preg_match("/,/", $city) OR preg_match("/,/", $start) OR preg_match("/,/", $city))
    {
        return false;
    }

    // determine url
    $url = "http://maps.googleapis.com/maps/api/directions/json?origin=$city_start&destination=$city_destination&sensor=false&mode=bicycling";
    echo $url;

    // open connection to google maps
    $curl_session = curl_init($url);
    curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl_session, CURLOPT_HEADER, 0);

    // get data from json output
    $json = curl_exec($curl_session);
    curl_close($curl_session);
    var_dump($json);
}

The above code returns a 400 error at var_dump, where $city, $start and $destination are respectively starting adress, destination adress and the city where the addresses belong to. The url stored in $url works alright and returns JSON output when entered in a browser.

Can anyone tell me what I am doing wrong?

Cheers,

D.

  • @MarcB He already is, check his code. Plus he even mentioned that the url stored in `$url` works when pasted in to a browser... – superphonic Jan 21 '14 at 15:23
  • Yeah, sorry, missed that totally... the joys of pseudo-monday here. – Marc B Jan 21 '14 at 15:25
  • Could you include an example of the finished URL that Curl is trying. – superphonic Jan 21 '14 at 15:34
  • In the code above the url is as follows: http://maps.googleapis.com/maps/api/directions/json?origin=H.J.E. Wenckebachweg 2550, Amsterdam&destination=Damrak 36, Amsterdam&sensor=false&mode=bicycling When I add '$url = urlencode($url);' between $url and echo it becomes: http%3A%2F%2Fmaps.googleapis.com%2Fmaps%2Fapi%2Fdirections%2Fjson%3Forigin%3DH.J.E.+Wenckebachweg+2550%2C+Amsterdam%26destination%3DDamrak+36%2C+Amsterdam%26sensor%3Dfalse%26mode%3Dbicycling – user3219795 Jan 21 '14 at 15:38
  • Yeah my mistake. Just the variables need encoding like my revised answer. – superphonic Jan 21 '14 at 15:42

1 Answers1

1

You could try and urlencode the variables:

$city_start = urlencode($start . ', ' . $city);
$city_destination = urlencode($destination . ', ' . $city);
superphonic
  • 7,954
  • 6
  • 30
  • 63
  • When I do that, the var_dump returns 'boolean false'. Unless I am missing something, that does not really help me get any further? Thanks for the effort though. =) – user3219795 Jan 21 '14 at 15:30
  • I can't imagine why, `urlencode()` should just replace spaces etc.. in the url with url friendly hex. – superphonic Jan 21 '14 at 15:32