首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法识别Mapquest API密钥

无法识别Mapquest API密钥
EN

Stack Overflow用户
提问于 2017-07-03 21:24:07
回答 1查看 540关注 0票数 1

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

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

消费者密钥消费者秘密

我点击了All Keys

我查找了Geocoding API Post Batch的文档,它说我应该将键作为参数之一。

我假设它是Consumer密钥,所以我包含了我的密钥。但是,当我进行调用时,我得到以下响应:

随此请求提交的AppKey无效。

我的代码:

代码语言:javascript
复制
$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:

代码语言:javascript
复制
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

以及调用的结果:

代码语言:javascript
复制
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] => 
)
EN

回答 1

Stack Overflow用户

发布于 2017-07-10 21:32:22

关键字需要在mapquestapi.com之后的url参数中,而不是post数据中。那你应该可以走了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44886735

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档