如何使用php rest api连接phonegap应用程序

时间:2014-12-24 20:51:12

标签: php android ajax rest

我用php(slim php micro framework)创建后端的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 app发布到服务器并且我写道:

// 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代码中可以看出,我写的一切都很好,但可能有什么问题?当我在我的Android手机上测试时,这就不行了。

1 个答案:

答案 0 :(得分:0)

我不知道你为什么使用&#39; php:// input&#39;);获取json数据。

简单。使用以下内容获取手机发送的json数据。

$request  = $app->request();
$body     = $request->getBody();
$input    = json_decode($body); 

要了解更多如何使用slim处理json数据,您可以尝试以下网站。 http://www.ibm.com/developerworks/library/x-slim-rest/