1

I signed up for the Mapquest API service, and I am now testing my application, using PHP.

Under Manage Keys, I created a new key, and Mapquest gave me:

Consumer Key Consumer Secret

I clicked on Approve All Keys

I looked up the documentation for Geocoding API Post Batch, and it says that I should include the key as one of the params.

I assumed it's the Consumer Key, so I included mine. However, when I make the call, I get the following response:

The AppKey submitted with this request is invalid.

My code:

$results = mapquest_v1_geocoding_batch_get_location(array('123 Main St, Anytown, WA', '123 Main St, Anytown, WA 98052'));
pretty_print($results);

function mapquest_v1_geocoding_batch_get_location($locations)
{
    //&location=Denver, CO&location=1555 Blake St, Denver, CO 80202&location=Boulder&key=KEY

    $postfields = array (
        'inFormat' => 'kvp',
        'outFormat' => 'json',
        'thumbMaps' => FALSE,
        'maxResults' => 1
    );
    $postfields_string = http_build_query($postfields);
    foreach ($locations as $location) {
        $postfields_string .= '&'.http_build_query(array('location' => $location));
    }
    $postfields_string .= '&'.http_build_query(array('key' => PARN_MAPQUEST_TW_TO_FB_KEY));
    pretty_echo($postfields_string);
    $url = 'https://www.mapquestapi.com/geocoding/v1/batch';       
    return jhm_curl_post_call($url, $postfields);
}

function jhm_curl_post_call($url, $postfields, $setopts_array = FALSE)
{
    $results = array();
    if (!$setopts_array) {
        $setopts_array = array();
    }
    if (!isset($setopts_array[CURLOPT_RETURNTRANSFER])) {
        $setopts_array[CURLOPT_RETURNTRANSFER] = TRUE;
    }
    if (!isset($setopts_array[CURLOPT_POST])) {
        $setopts_array[CURLOPT_POST] = TRUE;
    }    
    $setopts_array[CURLOPT_URL] = $url;
    $setopts_array[CURLOPT_POSTFIELDS] =  http_build_query($postfields);
    $ch = curl_init();
    curl_setopt_array ($ch , $setopts_array);
    $results['json_response'] = curl_exec($ch);
    $results['response'] = json_decode($results['json_response'], TRUE);
    $results['info'] = curl_getinfo($ch);
    $results['curl_errno'] = curl_errno($ch);
    $results['curl_error'] = curl_error($ch);
    curl_close($ch);
    return $results;
}

This is the $postfields_string:

inFormat=kvp&outFormat=json&thumbMaps=0&maxResults=1&location=123+Main+St%2C+Anytown%2C+WA&location=123+Main+St%2C+Anytown%2C+WA+98052&key=xxxxxxxxxxxxxxxxxxxxxxxxxxxx

and the results of the call:

Array
(
    [json_response] => The AppKey submitted with this request is invalid.
    [info] => Array
        (
            [url] => https://www.mapquestapi.com/geocoding/v1/batch
            [content_type] => text/plain
            [http_code] => 403
            [header_size] => 236
            [request_size] => 198
            [filetime] => -1
            [ssl_verify_result] => 0
            [redirect_count] => 0
            [total_time] => 0.265
            [namelookup_time] => 0.062
            [connect_time] => 0.109
            [pretransfer_time] => 0.203
            [size_upload] => 52
            [size_download] => 50
            [speed_download] => 188
            [speed_upload] => 196
            [download_content_length] => 50
            [upload_content_length] => 52
            [starttransfer_time] => 0.265
            [redirect_time] => 0
            [redirect_url] => 
            [primary_ip] => 207.200.103.5
            [certinfo] => Array
                (
                )

            [primary_port] => 443
            [local_ip] => 192.168.1.4
            [local_port] => 50514
        )

    [curl_errno] => 0
    [curl_error] => 
)
EastsideDev
  • 6,257
  • 9
  • 59
  • 116

1 Answers1

0

The key needs to be in the url parameters after mapquestapi.com rather than in the post data. Then you should be good to go.

MQBrian
  • 452
  • 1
  • 3
  • 7
  • This makes no sense. The API end point, does not include the key – EastsideDev Jul 10 '17 at 18:04
  • The post url contains the key (http://www.mapquestapi.com/geocoding/v1/batch?key=KEY) and the post body contains the addresses to be geocoded ({"locations": [{"city": "Denver", "state": "CO"},{"city": "Boulder", "state": "CO"}],"options": {"maxResults": -1, "thumbMaps": true, "ignoreLatLngInput": false}} ). There is more information on the batch geocode post user guide page. https://developer.mapquest.com/documentation/geocoding-api/batch/post/ – MQBrian Jul 11 '17 at 13:21