我在后端使用php(slim php微框架)创建了REST api。所以代码是:索引
.php$app->post('/coords', 'authenticate', function() use ($app) {
$response = array();
//$jsonData = $app->request->post('podaci');
$jsonData = @file_get_contents('php://input');
$aData = json_decode($jsonData);
$latitude = $aData->location->latitude;
$longitude = $aData->location->longitude;
$podaci = $latitude.' ddd '.$longitude;
global $user_id;
$db = new DbHandler();
// creating new task
$coords_id = $db->createCoords($user_id, $podaci);
if ($coords_id != NULL) {
$response["error"] = false;
$response["message"] = "Coords insert successfully";
echoRespnse(201, $response);
} else {
$response["error"] = true;
$response["message"] = "Coords not inserted";
echoRespnse(200, $response);
}
});
我还有DBhandler文件,代码(函数创建坐标):
public function createCoords($user_id, $podaci) {
$stmt = $this->conn->prepare("INSERT INTO coords(podaci) VALUES(?)");
$stmt->bind_param("s", $podaci);
$result = $stmt->execute();
$stmt->close();
}
我尝试使用带有ajax请求的示例数据进行此REST api,并且工作良好,但是现在我需要将数据从phonegap应用程序发布到服务器,然后我写道:
// BackgroundGeoLocation is highly configurable.
bgGeo.configure(callbackFn, failureFn, {
url: 'http://agroagro.com/agroMobile/v1/coords', // <-- Android ONLY: your server url to send locations to
params: {
Auth: 'df6017abde2d2re560896b63a0ee1039', // <-- Android ONLY: HTTP POST params sent to your server when persisting locations.
foo: 'bar' // <-- Android ONLY: HTTP POST params sent to your server when persisting locations.
},
desiredAccuracy: 0,
stationaryRadius: 50,
distanceFilter: 50,
notificationTitle: 'Background tracking', // <-- android only, customize the title of the notification
notificationText: 'ENABLED', // <-- android only, customize the text of the notification
activityType: 'AutomotiveNavigation',
debug: true, // <-- enable this hear sounds for background-geolocation life-cycle.
stopOnTerminate: false // <-- enable this to clear background location settings when the app terminates
});
我在这里阅读了如何制作 php 文件以获取输入数据:https://github.com/christocracy/cordova-plugin-background-geolocation/issues/79
正如你从我的索引中看到的.php代码我写得很好,但有什么问题?当我在安卓手机上进行测试时,这不起作用。
我不知道你为什么使用'php://input'); 用于获取 json 数据。
它很简单。 使用以下命令获取从手机发送的 JSON 数据。
$request = $app->request();
$body = $request->getBody();
$input = json_decode($body);
要了解有关如何使用slim处理json数据的更多信息,您可以尝试以下网站。http://www.ibm.com/developerworks/library/x-slim-rest/