Hardware Reference
In-Depth Information
Even though the results of this approach aren't
as pretty in a browser, it's very simple to extract
the date from within a Processing program—
or even a microcontroller program. Just look for the <
character in the text received from the server, read every-
thing until you get to the > character, and you've got it.
In this case, you're sending two parameters, name and
age . Their values are “tom” and “14”, respectively. You can
add as many parameters as you want, separating them
with the ampersand ( & ).
There are predefined variables in PHP that give you access
to these parameters and other aspects of the exchange
between client and server. The variable $_REQUEST , used
below, is an example that returns the parameters after
the question mark in an HTTP request. Other predefined
variables give you information about the client's browser
and operating system, the server's operating system, any
files the client's trying to upload, and much more.
HTTP requests don't just request files. You can add param-
eters to your request. If the URL you're requesting is
actually a program (like a PHP script), it can do something
with those parameters. To add parameters to a request,
add a question mark at the end of the request and param-
eters after that. Here's an example:
http:// www.example.com /get-parameters.php?name=tom&age=14
Test It
Here's a PHP script that
reads all the values sent
in via a request and prints them out.
<?php
/*
Parameter reader
Context: PHP
Save this script to your server as get-
parameters.php, and view it in a browser
using the URL shown earlier (you may
need to modify the path to the file if
you've put it in a subdirectory). You
should get a page that says:
Prints any parameters sent in using an HTTP GET command.
*/
// print out all the variables:
foreach ($_REQUEST as $key => $value)
{
echo "$key: $value<br>\n";
}
?>
name: tom
age: 14
You could also request it from
telnet or PuTTY like you did
earlier (be sure to include the
?name=tom&age=14 at the end of
the argument to GET, as in GET /get-
parameters.php?name=tom&age=14 ).
You'd get something similar to the code
at right, shown here with the HTTP
header.
8
HTTP/1.1 200 OK
Date: Thu, 15 Mar 2007 15:10:51 GMT
Server: Apache
X-Powered-By: PHP/5.1.2
Vary: Accept-Encoding
Connection: close
Content-Type: text/html; charset=UTF-8
name: tom<br>
age: 14<br>
 
Search WWH ::




Custom Search