Re: convert array to HTML GET request

From: Date: Sun, 02 Oct 2005 19:38:04 +0000
Subject: Re: convert array to HTML GET request
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-223532@lists.php.net to get a copy of this message
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"; } $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. -- Jasper Bryant-Greene Freelance web developer http://jasper.bryant-greene.name/

« previous php.general (#223532) next »