【问题标题】:How to connect phonegap app with php rest api如何将phonegap应用程序与php rest api连接起来
【发布时间】:2014-12-24 20:51:12
【问题描述】:

我在后端有 REST api,使用 php(slim php 微框架)创建。所以代码是: 索引.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 文件以获取 INPUT 数据:https://github.com/christocracy/cordova-plugin-background-geolocation/issues/79

正如您从我的 index.php 代码中看到的那样,我写的一切都很好,但可能有什么问题?当我在我的安卓手机上测试时,这不起作用。

【问题讨论】:

    标签: php android ajax rest


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-29
      • 2019-11-06
      • 1970-01-01
      • 1970-01-01
      • 2018-10-30
      • 1970-01-01
      • 2018-01-17
      • 1970-01-01
      相关资源
      最近更新 更多