繁体   English   中英

如何将PhoneGap应用与php Rest API连接

[英]How to connect phonegap app with php rest api

我在后端使用php(超薄php微框架)创建了REST api。 所以代码是:INDEX.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文件,代码(函数createCoords):

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

正如您从index.php代码中看到的那样,我写的都不错,但是可能是什么问题呢? 当我在Android手机上测试时,这根本不起作用。

我不为什么你要使用'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/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM