0

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 !

5
  • Are you sure this is json encoded? Did you encode 99 or something else? Commented Sep 17, 2013 at 3:05
  • I think it's urlencode(), and will output [Object object] Commented Sep 17, 2013 at 3:11
  • Wait... are you trying to read the variable from the URL with PHP or create a query string from some variables in PHP? Commented Sep 17, 2013 at 3:12
  • What makes you think that the URL is not what Chrome says the URL is? Commented Sep 17, 2013 at 6:28
  • I see the web page is test/rdp3.php/99, and I open the chrome's network whitch show the Request URL is test/rdp3.php/%5Bobject%20Object%5D Commented Sep 17, 2013 at 6:45

2 Answers 2

1

$_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
Sign up to request clarification or add additional context in comments.

2 Comments

but I get the "/[object Object]"
yes,the error is whitch I tried to send javascript object ,not like “99”,anyway ,thanks for your help
0

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'];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.