【问题标题】:Read json data with get method from from php api从 php api 使用 get 方法读取 json 数据
【发布时间】:2015-09-18 20:19:31
【问题描述】:

如何从服务器端读取用户名和密码。如果我使用 POST 方法而不是 GET,那么我可以通过

轻松阅读
$REQUEST = file_get_contents('php://input');
$REQUEST = json_decode($REQUEST, true);
echo json_encode($REQUEST);

这段代码。我可以得到回应 {“用户名”:“muradcse1”,“密码”:“1122”}

但是如果方法是get,那么读取json格式数据的方法是什么?我也需要这种格式的 get 方法。

请帮忙。

【问题讨论】:

  • json_decode() 将允许您将所有 json 数据呈现到 php 数组中。
  • 谢谢@Idealcastle 我应该在 json_decode() 函数中传递什么?假设 $REQUEST = file_get_contents('php://input'); $REQUEST = json_decode($REQUEST, true); "file_get_contents('php://input')" 不适用于 get 方法,但适用于 post。

标签: php rest get


【解决方案1】:

如果您问“如何从传入的 GET 请求中获取变量到我的服务器?”,那么您需要专门设置这些 GET 变量。
示例:

<?php

$username = $_GET['username']; //GET the username
$password = $_GET['password']; //GET the password

//Assign to array print as JSON
$json = array(
  'username' => $username,
  'password' => $password
);
echo json_encode($json); //Print JSON

作为 GET 请求提交到您的服务器的数据不会作为 JSON 提交。而是作为 URL_Encoded 提交。

【讨论】:

  • 谢谢你,所以你说不能像我的附件那样发送请求。要么我必须使用youtulee.com/…**** 或者,如果我想使用 {"username": "muradcse1", "password": "1122"} 格式来传递用户名和密码,那么我必须使用 POST 或别的东西,但不是 GET。不是吗?
  • 没错,GET请求的参数是在URL中发送的。 site.com/?username=john&amp;password=doe
  • 非常感谢@sudo soul
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-17
  • 1970-01-01
  • 2012-08-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多