简体   繁体   English

php:从POST接收json并保存到文件

[英]php: receive json from POST and save to file

I want to receive a POST request from a JS client with a json body (ie this is not form data), and save the .gigs (javascript) array to a file, after checking the .password field. 我想从JSON主体的JS客户端接收POST请求(即,这不是表单数据),并在检查.password字段后将.gigs(javascript)数组保存到文件中。 This is all my code (based on Receive JSON POST with PHP ) 这就是我所有的代码(基于PHP的Receive JSON POST

$json_params = file_get_contents("php://input");

if (strlen($json_params) > 0 && isValidJSON($json_params)){
    /* json_decode(..., true) returns an 'array', not an 'object'
   * Working combination: json_decode($json_params) WITH $inp->password
   */
  $inp = json_decode($json_params);
} else {
    echo "could not decode POST body";
    return;
}

$password = $inp->password;
// echo $password;

if ($password == "****") {
  $gigs = $inp['gigs'];
  // WAS $res = file_put_contents('gigs.json', json_encode($gigs), TEXT_FILE);
  $res = file_put_contents('gigs.json', json_encode($gigs));

  if ($res > 0) {
    echo "Success";
    return;
  } else {
    if (!$res) {
      http_response_code(500);
      echo "file_put_contents error:".$res;
      return;
    } else {
      http_response_code(500);
      echo "Error: saved zero data!";
      return;
    }
  }
} 
else {
  // http_response_code(403);      // (2)
  echo "Password invalid";
  return;
}

What I find is that 我发现的是

  • if I comment out the if statement and uncomment echo $password; 如果我注释掉if语句并且取消注释,则echo $password; then the right password is there 然后正确的密码在那里
  • if I uncomment line 2, which I want to do, then I get back a 500 and the error logs refer an Illegal string offset 'password' in line (1) above. 如果我取消第2行的注释,则返回500,并且错误日志引用上面第(1)行中的Illegal string offset 'password' Without that I get back a "Success" (all for the same password). 否则,我将返回"Success" (所有密码都相同)。

I don't understand what is happening, nor how to get 200, 403 and 500 error messages safely. 我不知道发生了什么,也不知道如何安全地获取200、403和500错误消息。

Note 注意

$json_params = file_get_contents("php://input");

If your scripts are running upon regular HTTP requests, passing data like it comes from HTML form, them you should consider using $_POST for your content, not php://input . 如果您的脚本在常规HTTP请求上运行,并且传递数据(例如来自HTML表单的数据),则应考虑使用$_POST作为内容,而不是php://input If you expect JSON in request body, then I'd be fine, yet I'd also check content type for application/json . 如果您希望请求正文中包含JSON,那么我会好的,但是我还要检查application/json内容类型。

Next: 下一个:

$inp = "I never got set";

if (strlen($json_params) > 0 && isValidJSON($json_params)){
  $inp = json_decode($json_params, true);
}

$password = $inp->password;
$password = $inp['password'];

This is pretty broken. 这很坏。 First, see json_decode() arguments (2nd) -> you are decoding to array ( true ), not object ( false ), so only $password = $inp['password']; 首先,请参阅json_decode()参数(第二个)->您要解码为数组( true ),而不是对象( false ),因此只有$password = $inp['password']; will work in your case. 将适合您的情况。 Also the whole code will fail when your input data is invalid as in that case $np is rubbish string, not the array you try to read later on. 同样,当您输入的数据无效时,整个代码也会失败,因为在这种情况下, $np是垃圾字符串,而不是您稍后尝试读取的数组。 Use null as default value and check for that prior further use. 使用null作为默认值,并检查该优先级是否可以进一步使用。

Next: 下一个:

$res = file_put_contents('gigs.json', json_encode($gigs), FILE_TEXT);

there's no FILE_TEXT option for file_put_contents() . 没有file_put_contents() FILE_TEXT选项。 Nor you'd need one. 您也不需要一个。

Once you correct these you'd be fine. 一旦纠正这些,就可以了。 Also print_r() and var_dump() may be the functions you wish to get familiar with for your further debugging. 另外, print_r()var_dump()可能是您希望进一步调试时熟悉的函数。

In general http://php.net/ -> lookup for functions you are about to use. 通常, http://php.net/- >查找要使用的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM