首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >当POST或PUT方法中使用参数时,file_get_content(‘php:/put’)为空

当POST或PUT方法中使用参数时,file_get_content(‘php:/put’)为空
EN

Stack Overflow用户
提问于 2018-07-18 05:34:08
回答 1查看 0关注 0票数 0

当我使用post和put方法时,file_get_contents('php:// input')为空。我的put结尾的一个例子看起来像这样:/ v2 / table?tablecolumn = 7&anothertablecolumn = 4

所有方法现在都通过Postman发送。

这是我的api尝试做的一个例子

以下是快速查看的简化版本:

代码语言:javascript
复制
if (($this->method === 'POST') || ($this->method === 'PUT')) {
  $json = file_get_contents('php://input');
  if(empty($json)) {
    $this->rc = 211;
    return;
  }
}
EN

回答 1

Stack Overflow用户

发布于 2018-07-18 14:58:34

看起来你正在尝试以JSON格式发送和接收数据。总是更好地使用、尝试、捕捉和进行解析。

希望能帮上忙。

代码语言:txt
复制
if(strcasecmp($_SERVER['REQUEST_METHOD'], 'POST') != 0){
    echo ('Request method must be POST!');
    throw new Exception('Request method must be POST!');
}
$contentType = isset($_SERVER["CONTENT_TYPE"]) ? trim($_SERVER["CONTENT_TYPE"]) : '';
if(strcasecmp($contentType, 'application/json') != 0){
    echo ('Content type must be: application/json');
    throw new Exception('Content type must be: application/json');
}

try{
    $Obj = json_decode(file_get_contents('php://input'), true);
}catch(Exception $e){
  echo json_encode('{"result":"FALSE","message":"Caught exception: '.
     $e->getMessage().' ~"}');
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100005594

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档