Articles

The month of PHP functions : building URL

  • Ecrit par Damien Seguy
  • vendredi 27 avril 2007
Image pour le titre du contenu

Ce document est aussi disponible en français fr 


Building a URL string is pretty easy : we just need the name of the variables, link them to their value, and concatenate the whole of it with a & sign. And don't forget to encode everything. Even the array. And recursivement. And all those strange characters...

Finally, this is a job for a super-hero : http_build_query().


http_build_query() will build a query string for a URL. This string is the ending part of a URL, after the final interrogation point, in a GET URL :

http://www.nexen.net/index.php?x=2

http_build_query() takes a full array, where the index values are the name of the variables, and the associated value is the actual value. The function will then build the query string, protecting everything, and recursively.

<?php

$vars = array('variable' => 'value',
              'string name' => 'to be protégée',
              'array[]' => '0',
              'array[]' => 2,
              'otherarray' => array('a','b','c'),
              'deeparray' => array('a'=> array('b')),
              'and now the Chinese' => '我爱你');
              
print http_buil_query($vars);

?>

The result is :

variable=value&string+name=to+be+prot%C3%A9g%C3%A9e&array%5B%5D=2&otherarray%5B0%5D=a&
otherarray%5B1%5D=b&otherarray%5B2%5D=c&deeparray%5Ba%5D%5B0%5D=b&
and+now+the+Chinese=%E6%88%91%E7%88%B1%E4%BD%A0

Onje just has to add this string to an URL, or to use it in a POST method, to send it to a distant web site. For example, if you want to set up a basic proxy, you may use this :

<?php
header("Location: /newpage.php?".http_build_query($_GET));
?>

You may also make a POST to GET converter :


<?php
print file_get_contents("http://www.votresite.com/index.php?".http_build_query($_POST));
?>

Upon reception PHP will analyse all this, and hand it to you in the  $_GET variable. If you need to decrypt this string within a PHP script, you just have to use the same internal fonction than PHP : parse_str().

<?php
parse_str('variable=valeur&cha%C3%AEne=%C3%A0+prot%C3%A9ger&tableau%5B%5D=2&autretableau%5B0%5D=a&autretableau%5B1%5D=b&autretableau%5B2%5D=c&tableaurecursif%5Ba%5D%5B0%5D=b&et+chinois=%E6%88%91%E7%88%B1%E4%BD%A0', $r);
print_r($r);
?>

Array
(
    [variable] => value
    [string_name] => to be protégée
    [array] => Array
        (
            [0] => 2
        )

    [otherarray] => Array
        (
            [0] => a
            [1] => b
            [2] => c
        )

    [deeparray] => Array
        (
            [a] => Array
                (
                    [0] => b
                )

        )

    [and_now_the_Chinese] => 我爱你
)
?>

Keep in mind
  • parse_str() take a variable as second argument, to hold the parsed values. Otherwise, they will be created directly in the main body of  the script.
  • parse_str() is not protected against the hugely long querys that create deep arrays.
  • A GET  URL is usally limited to a few kilobytes. Don't go beyond this limit, as the browser will probably cut the URL.
  • http_build_query() just like parse_str() are the same functions that PHP use internally.
< Précédent   Suivant >

Commentaires

Vous pouvez ajouter votre commentaire!


Vous devez vous connecter pour commenter