应用程序 /JSON 从 URL 获取参数


application /json get parameter from url

我是 php 的新手,

我想从网址获取参数,

我的请求标头是application/json

Chrome 的网络显示请求网址

test/rdp3.php/%5Bobject%20Object%5D  

事实上它是

test/rdp3.php/99

PHP代码

<?php
  $value = json_decode(file_get_contents('php://input'));
  echo $value->sessionName;
?>

如何获取网址参数(99) ?

我搜索它,但我找不到任何关于它的信息,

请帮忙!非常感谢!

$_SERVER['PATH_INFO']将返回/99 。然后,您可以使用trim()substr()删除/

'PATH_INFO'
    Contains any client-provided pathname information trailing the actual script 
    filename but preceding the query string, if available. For instance, if the 
    current script was accessed via the URL 
    http://www.example.com/php/path_info.php/some/stuff?foo=bar, then 
    $_SERVER['PATH_INFO'] would contain /some/stuff.

与 http://php.net/manual/en/reserved.variables.server.php 相比

更新

根据您的评论,我对您的确切尝试有点困惑。如果你正在取回[对象对象],这意味着你试图发送一个JavaScript对象作为URI的一部分。我建议对任何json数据使用HTTP请求正文。如果您打算使用 URI 来唯一标识要发送到服务器的数据(如"99"),则上面的代码将帮助您解析 URI。如果您想知道如何解析 HTTP 请求有效负载,那么以下代码将有所帮助。

从命令行使用 json 的 POST 请求示例:

curl -i -X POST -d '{"a": 1, "b": "abc"}' http://localhost:8080/test/rdp3.php/99 

使用 PHP 解析 json 对象:

<?php
$data = json_decode(file_get_contents("php://input"));
var_dump($data); // $data is of type stdClass so it can be treated like an object.
var_dump($data->a); // => 1
var_dump($data->b); // => abcd

URL test/rdp3.php/99格式不正确。

您要做的是在 url 的末尾设置一个键和一个值。所以test/rdp3.php/99test/rdp3.php?key=value.?表示查询字符串的开头。然后,每个键/值对用 & 分隔。

因此,您可以拥有如下所示的网址:

test/rdp3.php?key=value&id=99

然后在你的 PHP 代码中获取你所做的key的值:

$variableName = $_GET['key'];

要获取id的值,您需要执行以下操作:

$variableName2 = $_GET['id'];