2014-12-24 40 views
1

我在用php(slim php微型框架)創建的後端使用REST api。因此,代碼是: 的index.php如何將電話應用程序連接到php rest api

$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手機上測試時,這只是不工作。

回答

0

我不是你爲什麼使用'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/

+0

我現在就試試.. –

相關問題