views:

608

answers:

5

Okay, so I've written a REST API implementation using mod_rewrite and PHP. I'm accepting a query string via the body of HTTP DELETE requests (... collective groan?). Arguments about the wisdom of both previous statements aside, what I've found is that PHP doesn't automatically parse the request body of DELETE requests (i.e. $_POST is empty despite form-encoded query string appearing in body of request). This didn't particularly surprise me. What I did find surprising was that I've been unable to find a built-in PHP function for parsing a query string?? Have I simply overlooked something? I can do something like:

public function parseQS($queryString, &$postArray){
  $queryArray = explode('&', $queryString);
  for($i = 0; $i < count($queryArray); $i++) {
    $thisElement = split('=', $queryArray[$i]);
    $postArray[$thisElement[0]] = htmlspecialchars(urldecode($thisElement[1]));
  }
}

... it just seems odd that there wouldn't be a PHP built-in to handle this. Also, I suspect I shouldn't be using htmlspecialcharacters & urldecode to scrub form-encoded values... it's a different kind of encoding, but I'm also having trouble discerning which PHP function I should be using to decode form-encoded data.

Any suggestions will be appreciated.

+4  A: 

There's parse_str. Bad name, but does what you want. And notice that it returns nothing, the second argument is passed by reference.

Ionuț G. Stan
+1  A: 

You can use the parse_str function:

parse_str($queryString, $args);
Gumbo
A: 
public function parseQS($queryString, &$postArray){
  parse_str($queryString, $postArray);
}

;-)

Vincent Robert
lol... apparently.
codemonkey
someone vote this down? he was clearly kidding... not really suggesting i wrap the built-in using my function.
codemonkey
+3  A: 

There is a function that does it - http://php.net/parse_str. Since PHP has to do this for itself, there's no reason not to also open it up for use in the API.

Parses the string into variables void parse_str ( string $str [, array &$arr])

Parses str as if it were the query string passed via a URL and sets variables in the current scope.

<?php
$str = "first=value&arr[]=foo+bar&arr[]=baz";

parse_str($str, $output);
echo $output['first'];  // value
echo $output['arr'][0]; // foo bar
echo $output['arr'][1]; // baz
Alister Bulman
+2  A: 

http://us2.php.net/manual/en/function.parse-url.php

parse_url will help you grab the portion of the DOCUMENT_URI that contains the actual query.

You can then pass that section off to parse_str to extract individual elements from the query.

http://us2.php.net/manual/en/function.parse-str.php

Nolte Burke
interesting... I wonder whether there's an advantage of using this instead of @file_get_contents('php://input')... which is what I'm using currently.
codemonkey
php://stdin would offer some backwards compatibility with CLI interfaces. You would have to handle for the differences between straight terminal input and the HTTP headers, but it is possible. Generally speaking, it's always best to default to the built-in functions as they have been extensively optimized and will be well supported for the foreseeable future. Using "best practices" also results in more readable code. Using my suggestion you can do your task in just a few lines.
Nolte Burke