我已经能够使用以下格式执行解析以将数据发布到我的api:
工作样本:
<?php
$appId = 'XXXXXXXXXXXXXXXXXXXXXXXXXXX';
$private_token = 'XXXXXXXXXXXXXXXXXXX';
$geo_post = "/api/v1/geofence/";
$name = "Home";
$latitude = 38.646322;
$longitude = -121.185837;
$radius = 50;
$data = array(
"name" => $name,
"location" =>
array( "latitude" => $latitude, "longitude" => $longitude),
"matchRange" => $radius
);
$data_string = json_encode($data);
$ch = curl_init();
$headers = array(
'Content-Type:application/json',
'Authorization: Basic '. base64_encode($appId.":".$private_token) // <---
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, 'https://admin.plotprojects.com' . $geo_post );
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$content = trim(curl_exec($ch));
curl_close($ch);
//print_r($content);
?>
带有错误消息的示例:
<?php
if (isset($_REQUEST['add_new'])){
$appId = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$private_token = 'XXXXXXXXXXXXXXXXXXXXXXX';
$geo_post = "/api/v1/geofence/";
$name = $_GET['db_geofencename'];
$latitude = $_GET['db_latitude'];
$longitude = $_GET['db_longitude'];
$radius = $_GET['db_radius'];
$data = array(
"name" => $name,
"location" =>
array( "latitude" => $latitude, "longitude" => $longitude),
"matchRange" => $radius
);
$data_string = json_encode($data);
$ch = curl_init();
$headers = array(
'Content-Type:application/json',
'Authorization: Basic '. base64_encode($appId.":".$private_token) // <---
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, 'https://admin.plotprojects.com' . $geo_post );
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$content = trim(curl_exec($ch));
curl_close($ch);
print_r($content);
?>
正如你所看到我在我的$ data数组中使用了一个字符串,所以我试图从表单中捕获信息并将其传递给$ data数组,但如果我使用$ _GET或$ _POST我最终收到错误消息:
{“success”:false,“errorMessage”:“请求内容格式不正确:\ n预期字符串为JsString,但为空”,“errorCode”:“BadRequest”}
我做错了什么?
答案 0 :(得分:0)
所以我能想到的最好的是将你的$ latitude和$经度转换($ cast)为浮动数字。不要依赖于php自动类型转换,因为有时在内部将它保持为字符串,我猜api想要$ latitude和$ lon ...作为浮动数字而不是字符串。