Generate URL-encoded (GET) Query Strings With PHP March 4, 2012
Posted by tournasdimitrios1 in PHP.trackback
One of the easiest methods of passing information between pages is to pass the information in a query string on the end of the address of the page you are calling . To do this we need to add the information to be passed to the end of the URL-link . The query string containing the values to be passed is separated from the address of the page that we are passing the parameters to by a question mark (?) , each parameter being passed is separated from the next by an ampersand (&) , the name of each field is separated from the value of the field by an equals sign (=) , and any non alphanumeric characters in the field values is “escaped” (eg. spaces are replaced by %20) with PHP’s urlencode function . Lets take an example. You are to query a database and for that you need to send three variables via GET (country , city , id ) . The common way to pass them via GET is to construct a query string as below :
$country = "United States%&" ;
$city = "New York";
$country = urlencode($country) ;
$city = urlencode($city) ;
$id = 67554 ;
$query_string = "country={$country}&city={$city}&id={$id}";
$url = "http://www.example.com?" . $query_string;
echo $url ;
// Result
http://www.example.com?country=United+States%25%26&city=New+York&id=67554
The previously mentioned method is fine for three to four variables , but for more variables , the code gets hard to read and maintain and could generate subtle bugs in the constructed query .The best way to pass GET variables is to use PHP’s http_build_query() function available from PHP 5 , it takes an array of variables and builds a nice URL encoded string which you can append to an url . An example is shown below :
$country = "United States%&" ;
$city = "New York";
$id = 67554 ;
$fields = array('country' => $country ,
'city' => $city ,
'id' => $id );
$url = "http://www.example.com?" . http_build_query($fields, '', "&");
echo $url ;
//Resutl
http://www.example.com?country=United+States%25%26&city=New+York&id=67554
In the above example , an associative array where used to define our data . Alternatively , we could pass an indexed array containing only values and let the function fill the variable name using the array index and a variable you supply (the second parameter) . For example if you want to pass an array of six cities then your code is as follows :
$fields = array('Paris',
'New Yourk',
'Chin Hoa',
'Dublin',
'Rome');
$url = "http://www.example.php?" .
http_build_query($fields, 'cities', "&");
echo $url ;
//Result
http://www.example.php?cities0=Paris&cities1=New+Yourk&cities2=Chin+Hoa&cities3=Dublin&cities4=Rome
The third parameter to the function is not necessary as the function defaults to using ‘&’ as a separator .
You can also easily pass a complex array as below :
$city = "Moscow ";
$id = 7688;
$currency = "Rouble";
$total = 500 ;
$receipt_numr = "85993554";
$fields = array('city' => $city ,
'id' => $id ,
'paid' => array('currency' => $currency ,
'amount' => $total ,
'receipt' => $receipt_numr)
);
$url = "http://www.example.php?" .
http_build_query($fields, '', "&");
echo $url ;
//Result
http://www.example.php?city=Moscow+&id=7688&paid%5Bcurrency%5D=Rouble&paid%5Bamount%5D=500&paid%5Breceipt%5D=85993554
To go the reverse way , we can parse the encoded url above using PHP’s parse_url function , an example follows to demonstrate it’s usage :
$url_encoded = "http://www.example.php?city=Moscow+&id=7688&paid%5Bcurrency%5D=Rouble&paid%5Bamount%5D=500&paid%5Breceipt%5D=85993554 " ;
$parsed_url = parse_url($url_encoded);
echo "<pre>" ;
print_r($parsed_url) ;
echo "</pre>" ;
//Result
Array
(
[scheme] => http
[host] => www.example.php
[query] => city=Moscow+&id=7688&paid%5Bcurrency%5D=Rouble&paid%5Bamount%5D=500&paid%5Breceipt%5D=85993554
)
Of course , the result of the above example is useless in php , we have to take one more step and parse all submitted variables .
$url_encoded = "http://www.example.php?city=Moscow+&id=7688&paid%5Bcurrency%5D=Rouble&paid%5Bamount%5D=500&paid%5Breceipt%5D=85993554 " ;
$parsed_url = parse_url($url_encoded);
$url_query = $parsed_url['query'];
parse_str($url_query, $variable_values);
echo "<pre>" ;
print_r($variable_values);
echo "</pre>" ;
//Result
Array
(
[city] => Moscow
[id] => 7688
[paid] => Array
(
[currency] => Rouble
[amount] => 500
[receipt] => 85993554
)
)
I hope this article was helpful to you .

Linux >>> 

Thanks for that awesome posting. It saved MUCH time
same i have tried but i didn’t getting
@manoj
Could you be more descriptive ?