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!

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Plugin4U
  • 1
  • 4

3 Answers3

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
)
ewcz
  • 12,819
  • 1
  • 25
  • 47
  • @Plugin4U you can then access individual parameters via the array, i.e., `$arr['id']` will give you the value of the parameter `id` – ewcz Mar 18 '17 at 11:16
  • Why not simply use the `$_GET`-super global instead of parsing it yourself? – M. Eriksson Mar 18 '17 at 11:16
  • @MagnusEriksson sure, I thought that the question was about how to do it manually... :) – ewcz Mar 18 '17 at 11:17
  • 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. – M. Eriksson Mar 18 '17 at 11:19
0

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

E.g.:

$info = $_GET['id']
James Risner
  • 5,451
  • 11
  • 25
  • 47
Asperitas
  • 339
  • 6
  • 13
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"]);
Moustafa Elkady
  • 670
  • 1
  • 7
  • 17
  • _"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. – M. Eriksson Mar 18 '17 at 11:21