Re: convert array to HTML GET request

From: Date: Sun, 02 Oct 2005 20:50:19 +0000
Subject: Re: convert array to HTML GET request
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-223534@lists.php.net to get a copy of this message
Jasper Bryant-Greene wrote:
Martin van den Berg wrote:
How does one convert an array into a HTML GET request easely? Are there any standard functions? Same for HTML POST requests.
HTTP GET and POST requests are formatted the same, just sent in different ways. I don't know if there's a function; I'd just use: <?php $myArray = array(
    'apples' => 'oranges',
    'pears' => 'peaches',
); $queryParts = array(); foreach( $myArray as $key=>$value ) {
    $queryParts[] = "$key=$value";
you might also want to use urlencode() in here to make sure the URL stays valid.
- tul
} $query = implode( '&', $queryParts ); // For GET, use e.g.: $data = file_get_contents( "http://www.example.com/?$query" ); // For POST, pass $query to curl_setopt for an HTTP POST (see CURL docs) ?> Disclaimer: I typed this from memory and haven't tested it, so there might be minor errors, but you get the picture.


« previous php.general (#223534) next »