0

So I am probably making api system and I am trying to get the client req api. I checked some tutorials and I found I could use :

$data = $_SERVER['QUERY_STRING'];

It's works but I am getting string like : action=get&id=theapikeygoeshere. And I need to get only the text after id= , How can I do that?
Thanks!

3

3 Answers 3

0

parse the query string using parse_str:

<?php

$arr = array();
parse_str('action=get&id=theapikeygoeshere', $arr);
print_r($arr);

?>

This gives:

Array
(
    [action] => get
    [id] => theapikeygoeshere
)
Sign up to request clarification or add additional context in comments.

4 Comments

@Plugin4U you can then access individual parameters via the array, i.e., $arr['id'] will give you the value of the parameter id
Why not simply use the $_GET-super global instead of parsing it yourself?
@MagnusEriksson sure, I thought that the question was about how to do it manually... :)
I think it's more about not knowing about how to read the query string params. I'm struggling to find any use case where you would need to do it manually.
0

I think the best thing is to use $_GET['id'] but if you want to extract any thing from the QUERY_STRING use

parse_str($_SERVER["QUERY_STRING"], $output);
var_dump($output["id"]);

1 Comment

"but if you want to extract any thing from the QUERY_STRING"... that's what $_GET does. That code basically just building your own $_GET-array, which doesn't make much sense.
0

You can do so by using $_GET['id']. :) $_GET can be used for any URL parameters like those.

E.g.:

$info = $_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.