使用PHP cUrl调用MapBox API创建空数据集时遇Not Found错误求助
解决MapBox API创建空数据集的404错误
问题核心原因
收到{"message":"Not Found"}错误主要源于两个关键问题:
- 请求Payload格式错误:你将参数包裹成了数组
$data[] = array(...),但MapBox API要求提交单个JSON对象,而非数组结构。 - 请求配置细节疏漏:虽然URL结构大体正确,但请求方式的配置可以更规范,同时需确保access_token具备对应权限。
修正后的完整代码
$ski_datasets_create = $this->request->getPost(); // 直接构造单个JSON对象,而非数组 $data = array( 'name' => $this->request->getPost('dh_dataset_name'), 'description' => $this->request->getPost('dh_dataset_description') ); $payload = json_encode($data); // 替换为你的真实MapBox用户名和有效access_token $url = "https://api.mapbox.com/datasets/v1/你的用户名?access_token=你的授权令牌"; $request = curl_init(); curl_setopt($request, CURLOPT_URL, $url); curl_setopt($request, CURLOPT_RETURNTRANSFER, 1); // 直接使用CURLOPT_POST,比CUSTOMREQUEST更符合cURL规范 curl_setopt($request, CURLOPT_POST, true); curl_setopt($request, CURLOPT_POSTFIELDS, $payload); curl_setopt($request, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); $response = curl_exec($request); // 捕获并输出cURL错误信息,便于调试 $curl_errno = curl_errno($request); $curl_error = curl_error($request); echo '<pre>'; if ($curl_errno) { echo "cURL错误代码: {$curl_errno}\n错误信息: {$curl_error}\n"; } print_r($response); echo '</pre>'; // 记得关闭cURL资源 curl_close($request); die();
额外注意事项
- Access Token权限:确保你的access_token拥有
datasets:write权限,否则会返回权限错误而非404。 - 用户名准确性:URL中的用户名必须与你的MapBox账户完全一致,不能出现拼写错误。
- 必填字段验证:
name是创建数据集的必填项,需确保前端表单提交的dh_dataset_name非空。
内容的提问来源于stack exchange,提问作者spreaderman
相关产品推荐
相关产品推荐

