【问题标题】:JSON from URL returns null来自 URL 的 JSON 返回 null
【发布时间】:2019-01-21 22:45:30
【问题描述】:

我使用以下 URL 从 POSTMAN 调用 PHP 文件:http://localhost/st/user_login.php?surat=DRIVER&sandi=123

user_login.php 中,我输入这个来处理 JSON:

$json = file_get_contents('php://input');
$obj = json_decode($json,true);
$email = $obj['surat'];
$password = $obj['sandi'];

问题是,$email 总是返回一个空值。 谁能告诉我问题出在哪里?

【问题讨论】:

  • 您正在发送 GET 变量 - 您检查过 $_GET 吗?试试print_r($_GET),看看有没有你想要的数据
  • file_get_contents('php://input') 不会返回 JSON,因为这不是您发送给它的内容。您的数据在 $_GET 变量中。

标签: php json postman


【解决方案1】:

当我们使用php://inputPHP "php://input" vs $_POST

尝试使用$_GET$_POST。在您的情况下,显然您需要使用$_GET。但是,出于安全原因,我们最好使用$_POST

例如,使用 POST man 和 php://input

  • 正文:原始 - JSON(应用程序/json)
  • 发布

PHP 测试脚本

<?php

header('Content-Type: application/json');

$json = file_get_contents('php://input');
$obj = json_decode($json,true);

$result = [];
$result['message'] = 'Success Message';
$result['email'] = $obj['email'];
echo json_encode($result);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 2014-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多