{REQUEST_METHOD} {REQUEST_URI} HTTP/{PROTOCOL_VERSION}
Header: value
Another-Header: value
Message body
HTTP/{PROTOCOL_VERSION} {STATUS_CODE} {REASON_PHRASE}
Header: value
Another-Header: value
Message body
<scheme>://<host>(:<port>)?/<path>?<query string arguments>
name=value&name2=value2&etc
$version = $HTTP_SERVER_VARS['PROTOCOL_VERSION']
$method = $HTTP_SERVER_VARS['REQUEST_METHOD']
$url = $HTTP_SERVER_VARS['REQUEST_URI'];
// except when it isn't...
// Most headers, e.g. Accept:
$accept = $HTTP_SERVER_VARS['HTTP_ACCEPT'];
// Some headers, e.g. Content-Type:
$contentType = $HTTP_SERVER_VARS['CONTENT_TYPE'];
$headers = apache_request_headers();
// or its alias
$headers = getallheaders();
$contentType = $headers['Content-Type'];
// Assume /foo?duck=goose
echo $duck; // "goose"
// Also works for POST: assume cat=dog
echo $cat; // "dog"
// And cookies: assume bird=bat
echo $bird; // "bat"
// What if /foo?duck=goose
// AND POST duck=bluebird
// AND cookie duck=eagle ?
echo $duck; // ?
; Get -> Post -> Cookie
variables_order = "EGPCS"
// What if /foo?duck=goose
// AND POST duck=bluebird
// AND cookie duck=eagle ?
echo $duck; // "eagle"
$duck = $HTTP_POST_VARS['duck'];
echo $duck; // "bluebird"
$data = parse($HTTP_RAW_POST_DATA); // parse somehow...
$profit($data);
header('HTTP/1.0 404 Not Found'); // Send a status line
header('Location: /foo'); // Implicit 302
header('Location: /foo', true, 301); // Status code as argument
header('X-Foo: Bar');
header('X-Foo: Baz', false); // Emit an additional header
header('X-Foo: Bat'); // Replace any previous values
echo "Foo!";
<?php
$url = 'http://http://www.phpconference.com.br/';
$text = 'PHP Conference Brasil';
?>
<a href="<?php echo $url ?>"><?php echo $text ?></a>
Promote reuse of code across projects,
via "standards recommendations":
// Protocol
$request->getProtocolVersion();
// Method
$request->getMethod();
// Url
$request->getUrl();
foreach ($request->getHeaders() as $header => $values) {
printf("%s: %s\n", $header, implode(', ', $values);
}
$body = $request->getBody();
// Treat it as a string
echo $body;
// Or as a stream-like implementation
while (! $body->eof()) {
echo $body->read(4096);
}
$server = $request->getServerParams();
$cookie = $request->getCookieParams();
$query = $request->getQueryParams();
$data = $request->getBodyParams();
$files = $request->getFileParams();
// Protocol
$response->setProtocolVersion('1.0');
// Status
$response->setStatus(404, 'Not Found');
header(sprintf(
'HTTP/%s %d %s',
$response->getProtocolVersion(),
$response->getStatusCode(),
$response->getReasonPhrase()
));
$response->setHeader('X-Foo', 'Bar');
$response->addHeader('X-Foo', 'Baz'); // add another value!
$response->setHeader('X-Foo', 'Bat'); // overwrite!
foreach ($request->getHeaders() as $header => $values) {
foreach ($values as $value) {
header(sprintf(
'%s: %s',
$header,
$value
), false);
}
}
$body = $response->getBody();
$body->write('Foo! Bar!');
$body->write(' More!');
echo $body;
More specifically, start thinking in terms of middleware:
use Psr\Http\Message\IncomingRequestInterface as Request;
use Psr\Http\Message\OutgoingResponseInterface as Response;
$handler = function (Request $request, Response $response) {
// Grab input from the request
// Update the response
};
class ContactController
{
private $contact;
public function __construct(Contact $contact)
{
$this->contact = $contact;
}
public function dispatch(Request $req, Response $res)
{
return call_user_func($this->contact, $req, $res);
}
}
Developers are already working on a native PHP extension!