我有这个php代码,可将GeoJSON数据发布到名为AirMap的服务中,以检索用于飞行UAS的相关规则集。
index.php
<?
$ch = curl_init();
$json_data = '"geometry":' + '{ "type": "Polygon",
"coordinates": [
[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ]
]
}';
curl_setopt($ch, CURLOPT_URL, "https://api.airmap.com/rules/v1");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "$'{");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
$headers = array();
$headers[] = "X-Api-Key: (removed)";
$headers[] = "Content-Type: application/json; charset=utf-8";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
echo "$result";
?>
只要执行此代码,就会显示页面
“错误请求”
index.php 2
<?
$ch = curl_init();
$json_data = '{ "type": "Polygon",
"coordinates": [
[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ]
]
}';
curl_setopt($ch, CURLOPT_URL, "https://api.airmap.com/rules/v1");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "$'{");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
$headers = array();
$headers[] = "X-Api-Key: (removed)";
$headers[] = "Content-Type: application/json; charset=utf-8";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
echo "$result";
?>
每当我执行该代码时,它就会显示
{"status":"fail","data":{"message":"parameter validation failed","data":[{"name":"geometry","message":"Invalid param"},{"name":"geometry","message":"A valid geometry must be included."},{"name":"type","message":"Parameter not allowed."},{"name":"coordinates","message":"Parameter not allowed."}]}}
我试图理解这意味着什么,但我目前处于死胡同。文档(和错误代码)说“几何”部分是必需的,所以这就是我添加它的原因。该文档位于https://developers.airmap.com/docs/rulesets,我已经通过GeoJSON linter运行了GeoJSON,并通过了。
答案 0 :(得分:0)
这部分让我感到困惑:
curl_setopt($ch, CURLOPT_POSTFIELDS, "$'{");
[...]
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
您确定这两个都结合了吗?也许第一部分只是被覆盖?
更新
查看您的代码后,我发现其规则集示例与示例有以下区别:
$json_data = '"geometry":' + '{ "type": "Polygon",
"coordinates": [
[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ]
]
}';
这是错误的,因为+
不是用来连接字符串.
的。我也尝试用撇号纠正其他部分,结果是:
$json_data = '{"geometry": "{ \\"type\\": \\"Polygon\\",
\\"coordinates\\": [
[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ]
]}"
}';
答案 1 :(得分:0)
我联系了AirMap支持部门,他们帮助了我。由于它们需要与cURL请求一起传递的其他标志(几何形状),因此它们也需要打开和关闭花括号。所以几何部分看起来像这样:
$json_data2 = '{"geometry": { "type": "Polygon", "coordinates": [ [ [ -118.37099075317383, 33.85505651142062 ], [ -118.37305068969727, 33.85502978214579 ], [ -118.37347984313963, 33.854673391015496 ], [ -118.37306141853333, 33.85231226221667 ], [ -118.37193489074707, 33.85174201755203 ], [ -118.36997151374815, 33.85176874785573 ], [ -118.36995005607605, 33.8528112231754 ], [ -118.37099075317383, 33.85505651142062 ] ] ] }}';