iT邦幫忙

0

PHP web service使用POST方式接收Json

小斑 2019-01-28 16:29:5812279 瀏覽
  • 分享至 

  • xImage

以下是PHP web service使用POST方式接收Json的程式碼:

<?php
if($_SERVER['REQUEST_METHOD'] == "POST"){
    $inputJson = $_POST['inputJson'];
	if(!empty($inputJson)){
        //......
    }else{
		$json = array("status" => 1, "msg" => "inputJson is empty");
	}
}else{
		$json = array("status" => 1, "msg" => "Request method not accepted");
}
header('Content-type: application/json;charset=utf-8');
echo json_encode($json);
?>

有兩個問題想請教大家:
(1) 使用postman測試,卻回傳Request method not accepted?
https://ithelp.ithome.com.tw/upload/images/20190128/201064968Q84QN9Du5.png

(2) 把最外面的if else註解,仍回傳inputJson is empty?
https://ithelp.ithome.com.tw/upload/images/20190128/20106496BxtaQayPZD.png

還請指點方向......謝謝。

看更多先前的討論...收起先前的討論...
wingkawa iT邦新手 3 級 ‧ 2019-01-28 16:40:43 檢舉
1. 使用postman測試,卻回傳Request method not accepted?
$_SERVER['REQUEST_METHOD'] 印出來看看是什麼。
2. 把最外面的if else註解,仍回傳inputJson is empty?
$_POST['inputJson'] 印出來看看是什麼。
小斑 iT邦新手 3 級 ‧ 2019-01-28 16:56:03 檢舉
1. $_SERVER['REQUEST_METHOD'] = GET
2. $_POST['inputJson'] = null
wingkawa iT邦新手 3 級 ‧ 2019-01-28 16:58:42 檢舉
那就是啦,你根本沒有POST東西到API~
小斑 iT邦新手 3 級 ‧ 2019-01-28 17:15:39 檢舉
不好意思,一直傳不進去,請問應該怎麼改@@
wingkawa iT邦新手 3 級 ‧ 2019-01-28 17:17:09 檢舉
-
你得先告訴大家為什麼在$_POST內會加上inputJson?重點是這個索引名稱怎麼來的。
wingkawa iT邦新手 3 級 ‧ 2019-01-28 17:42:43 檢舉
首先要解決的問題是,明明postman設定POST,結果卻仍然是GET
postman有沒有動到其他設定,盡量貼出來看看
wingkawa iT邦新手 3 級 ‧ 2019-01-28 17:49:09 檢舉
URL直接指到你的程式?還是中間有轉址?
小斑 iT邦新手 3 級 ‧ 2019-01-28 17:51:43 檢舉
URL直接指到程式
小斑 iT邦新手 3 級 ‧ 2019-01-28 18:01:35 檢舉
盡量貼出來了,在下方,謝謝您
fillano iT邦超人 1 級 ‧ 2019-01-29 10:45:33 檢舉
$_POST收到的東西,是當表單格式是url encoded或是multipart form data時,PHP幫你剖析過的結果。但是post json時,body的內容是一個Json文件,PHP不會處理的,你必須直接去讀body的內容,然後把他decode。
wingkawa iT邦新手 3 級 ‧ 2019-01-29 13:42:40 檢舉
他現在最怪的地方是:
$_SERVER['REQUEST_METHOD'] = GET
先不管傳的是json還是什麼其它玩意兒,這東西印出來應該要是POST而不是GET
fillano iT邦超人 1 級 ‧ 2019-01-29 16:20:06 檢舉
也是...
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
0

沒看到你的$_POST['inputJson']; ?

小斑 iT邦新手 3 級 ‧ 2019-01-28 17:15:55 檢舉

不好意思,一直傳不進去,請問應該怎麼改@@

把全部的程式包括postman上的都放上來看看,不要放圖,
沒看到你post的code無法判斷

小斑 iT邦新手 3 級 ‧ 2019-01-28 17:32:22 檢舉

PS:先說聲不好意思,我在web service方面還是新手,如有缺失還請多包涵......

程式是全部了,另外,postman上的,請參考以下:
Headers:
Content-Type是application/json

Body:
選raw和JSON(application/json)
測試內容

{
	"A":"TEST1",
	"CON":{
		"AA":[
		"TEST",
		"TEST",
		"TEST"
		],
		"BB":[
		"TEST",
		"TEST",
		"TEST"
		]
	}
}
0
wingkawa
iT邦新手 3 級 ‧ 2019-01-29 09:19:12

按一下Code看看
https://ithelp.ithome.com.tw/upload/images/20190129/201094266JLRg7qYDU.png

1
rewrite
iT邦新手 3 級 ‧ 2019-01-29 09:21:58

依你的結構為主,你的結構想要接收json格式的資料,你用POST,code上是沒有問題,不過方法錯了

你要「傳入的值」是屬於"文本格式",跟$_POST & $_GET屬於key-value格式的處理方法不一樣

你可以改成用 I/O streams的方式處理傳入的值

<?php 
$json = array();
	
if($_SERVER['REQUEST_METHOD'] == "POST"){
  // 換成php://input 
  $inputJson = file_get_contents('php://input');
  if(!empty(json_decode($inputJson))){
    //......
  }else{
    $json = array("status" => 1, "msg" => "inputJson is empty");
  }
}else{
  $json = array("status" => 1, "msg" => "Request method not accepted");
}
header('Content-type: application/json;charset=utf-8');
echo json_encode($json);
?>

你用I/O streams的方式處理使用者傳入的資料要很謹慎(相較於POST來說),因為使用者可以傳任何的文本格式進場,
除非有特殊需要或是環境限制,不然是建議用key-value格式的傳輸格式,將每個欄位的型態跟長度定義好,再自行包裝json做處理

在postman上你要傳key-value可以用以下兩種格式
form-data - 可傳送的value包含檔案
x-www-form-urlencoded - 會對value進行encode的動作

以上兩種傳輸行為都可以透過$_POST來取值,這樣就跟你的寫法沒有太大差別,只要你的key對應到就可以取值。

0
海綿寶寶
iT邦大神 1 級 ‧ 2019-01-30 08:59:39

postman 使用 x-www-form-urlencoded 而不是 raw

參考這篇說明

我要發表回答

立即登入回答