iT邦幫忙

0

關於POSTMAN測試PHP

  • 分享至 

  • xImage
<?php

$result = [
    'success' => false,
    'resultCode' => 400,
    'errorMsg' => '資料不足',
];

if(! isset($from_me)){
    echo json_encode($result, JSON_UNESCAPED_UNICODE);
    exit;
}


$entityBody = file_get_contents('php://input');

$bdata = json_decode($entityBody, true);

$result['data_from'] = $bdata;

$reuire_fields = [
    'user_name',
    'user_phone',
    'user_email',
    'user_password',
];

foreach($reuire_fields as $rf){
    if(empty($bdata[$rf])){
        $result['resultCode'] = 405;
        $result['errorMsg'] = $rf. '為必要欄位';
        echo json_encode($result, JSON_UNESCAPED_UNICODE);
        exit;
    }
}


$sql = "UPDATE `user_data` SET 
                `user_name`=?,
                `user_phone`=?,
                `user_email`=?
                WHERE user_id=? AND `user_password`=?";

$stmt = $pdo->prepare($sql);

$stmt->execute([
    $bdata['user_name'],
    $bdata['user_phone'],
    $bdata['user_email'],
    $_SESSION['user']['user_id'],
    $bdata['user_password'],
]);
if($stmt->rowCount()==1){
    $result['success'] = true;
    $result['resultCode'] = 200;
    $result['errorMsg'] = '';
} else {
    $result['resultCode'] = 406;
    $result['errorMsg'] = '修改沒有成功';
}
echo json_encode($result, JSON_UNESCAPED_UNICODE);

以上是我的PHP的程式碼
我用POSTMAN FORM-DATA
這些欄位PUT給PHP (VALUE都有給值)
KEY
'user_name',
'user_phone',
'user_email',
'user_password',
條件是如果為空值才會跑到會讀出

   {"success":false,"resultCode":405,"errorMsg":"user_name為必要欄位","data_from":null}
更新
跑出這一段就表示他根本沒有接受到我POSTMAN PUT給他的值 
是我用錯方式嘛 QQ?

https://ithelp.ithome.com.tw/upload/images/20181123/20109246SKLTlyflQI.jpg

11/23
新增postman照片

youarefat iT邦新手 5 級 ‧ 2018-11-22 18:06:09 檢舉
咦?form-data是可以用file_get_contents('php://input')的嗎?
youarefat iT邦新手 5 級 ‧ 2018-11-22 18:06:24 檢舉
https://stackoverflow.com/questions/2731297/file-get-contentsphp-input-or-http-raw-post-data-which-one-is-better-to
Homura iT邦高手 1 級 ‧ 2018-11-22 22:02:24 檢舉
你postman畫面也貼一下吧!@@
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1
Neil
iT邦新手 5 級 ‧ 2018-11-22 21:16:08

https://lornajane.net/posts/2008/accessing-incoming-put-data-from-php
會不會是像文章內一樣要用這種方式取值?
parse_str(file_get_contents("php://input"),$post_vars);

我要發表回答

立即登入回答