2017-10-08 81 views
1

我試圖創建一個將在Android發送到文本文件數據的PHP腳本,但我無法得到輸出,因爲在PHP的 這裏有一些誤差是PHP代碼: -在PHP中讀取android中發送的JSON對象?

<?php 
    if($_SERVER["REQUEST_METHOD"]=="POST") 
    { 

     $data=$_POST["OrderSummary"]; 
     $json=json_decode($data,true); 
     file_put_contents("text.txt",$json); 

    }?> 

任何可以告訴我哪裏出錯了,我怎麼能corect它呢?

+3

請發佈錯誤消息 – Webdesigner

+1

您使用的是哪個版本的PHP? – Barns

+0

@ Barns52我使用php 5.6.30 –

回答

0

你可以試試這段代碼,它會給你更多關於腳本中發生了什麼的信息。

然後檢查你的android代碼中的響應。

<?php 
// array for JSON response 
$response = array(); 

// Check request method -- for some reason?? 
if($_SERVER["REQUEST_METHOD"]=="POST"){ 

    // check for required fields 
    if (isset($_POST['OrderSummary'])) { 

     $data = $_POST['OrderSummary']; 
     $json = json_decode($data,true); 

     $my_file = 'text.txt'; 

     $result = file_put_contents($my_file,$json); 
     $response["success"] = 1; 
     $response["message"] = "Number of bytes written = " .$results; 
    } 
    else{ 
     $response["success"] = 0; 
     $response["message"] = "Parameters not correctly formatted"; 
    } 
else{ 
    $response["success"] = 2; 
    $response["message"] = "Somebody is not using the POST request method!"; 
} 

echo json_encode($response); 
?> 

編輯:

我不明白爲什麼要檢查的請求方法。但是我仍然保留在PHP腳本中。

+0

謝謝@ Barns52它的工作原理,因爲我使用Android的休息API我不得不使用該職位。 –

+0

使用後可以 - 我也使用它,主要是。我只是不認爲你必須明確檢查方法。 – Barns

+0

該腳本現在重新編輯寫入文件中的以下數據:-ArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArray –