Reputation: 633
I am new with php ,
I want to get parameter from url ,
my request header is application/json
,
the chrome's network show Request URL
test/rdp3.php/%5Bobject%20Object%5D
and in fact it is
test/rdp3.php/99
PHP CODE
<?php
$value = json_decode(file_get_contents('php://input'));
echo $value->sessionName;
?>
how can I get url parameter(99) ?
I search it , but I can't find any information about that,
please help! thanks a lot !
Upvotes: 0
Views: 3574
Reputation: 101
$_SERVER['PATH_INFO']
will return /99
. You could then use trim()
or substr()
to remove the /
.
'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.
from http://php.net/manual/en/reserved.variables.server.php
Update
Based on your comment, I'm a bit confused on what you are attempting exactly. If you are getting [object Object] back that means that you tried to send a JavaScript object as part of the URI. I would suggest using the HTTP Request body for any json data. If you intend to use the URI to uniquely identify the data you are sending to the server (like '99'), then the code above will help you to parse the URI. If you are wondering how to parse an HTTP request payload, then the following code will help.
Example POST request using json from the command line:
curl -i -X POST -d '{"a": 1, "b": "abc"}' http://localhost:8080/test/rdp3.php/99
Using PHP to parse the json object:
<?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
Upvotes: 1
Reputation: 3654
The URL is test/rdp3.php/99
is malformed.
What you would want to do is set a key and a value at the end of the url. So test/rdp3.php/99
would be test/rdp3.php?key=value
. The ?
denotes the beginning of the query string. Each key/value pair is then separated by &
.
So you can have urls that look like this:
test/rdp3.php?key=value&id=99
Then in your PHP code to get the value of key
you do:
$variableName = $_GET['key'];
To get the value of id
you would do:
$variableName2 = $_GET['id'];
Upvotes: 0