无法识别Mapquest API密钥

时间:2017-07-03 13:24:07

标签: mapquest

我注册了Mapquest API服务,现在正在使用PHP测试我的应用程序。

在Manage Keys下,我创建了一个新密钥,Mapquest给了我:

消费者密钥 消费者秘密

我点击了批准所有密钥

我查看了Geocoding API Post Batch的文档,它说我应该将密钥作为一个参数包括在内。

我认为它是消费者密钥,所以我包括了我的。但是,当我拨打电话时,我得到以下回复:

随此请求提交的AppKey无效。

我的代码:

$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;
}

这是$ 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

和通话结果:

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] => 
)

1 个答案:

答案 0 :(得分:0)

密钥需要在mapquestapi.com之后的url参数中,而不是在post数据中。然后你应该好好去。

相关问题