{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>://(<user-info>@)<host>(:<port>)(/<path>)(?<query>)
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>
array(
'element_name' => array(
'tmp_name' => 'path where PHP uploaded it',
'name' => 'filename as presented by client',
'size' => 'file size as presented by client',
'type' => 'file MIME-type as presented by client',
'error' => 'appropriate UPLOAD_ERR_* code',
),
)
array(
'element_name' => array(
'tmp_name' => array( /* key for each element */ ),
'name' => array( /* key for each element */ ),
'size' => array( /* key for each element */ ),
'type' => array( /* key for each element */ ),
'error' => array( /* key for each element */ ),
),
)
path = environ.get("PATH_INFO", "")
query = parse_qs(environ.get("QUERY_STRING", ""))
accept = environ.get("HTTP_ACCEPT", "text/html")
content_length = int(environ.get("CONTENT_LENGTH", 0))
body = environ["wsgi.input"].read(content_length)
headers = [("Content-Type", "application/json",
("X-Foo", "Bar")]
start_response("200 OK", headers)
return stream_representing_body
request = Rack::Request.new env
uri = request.url
accept = request.env['HTTP_ACCEPT']
data = JSON.parse( request.body.read )
response = Rack::Response.new
response.status = 200
response['Content-Type'] = 'application/json'
response.write '{"message": "Content for message"}'
response.finish
var method = req.method;
var path = require('url').parse(req.url).pathname;
var query = require('url').parse(req.url, true).query;
var accept = req.headers.accept;
var body;
req.on('data', function(chunk) { body += chunk.toString(); });
req.on('end', function() { body = JSON.parse(body); });
res.statusCode = 200;
res.writeHead(200, 'OK!');
res.setHeader('Content-Type', 'application/json');
res.write('{"message": "Message for the body"}');
res.end();
Promote reuse of code across projects,
via "standards recommendations":
$body = new Stream();
$stream->write('{"foo":"bar"}');
$request = (new Request())
->withMethod('GET')
->withUri(new Uri('https://api.example.com/'))
->withHeader('Accept', 'application/json')
->withBody($stream);
$request = ServerRequestFactory::fromGlobals();
$method = $request->getMethod();
$path = $request->getUri()->getPath();
$accept = $request->getHeader('Accept');
$data = json_decode((string) $request->getBody());
$query = $request->getQueryParams();
$cookies = $request->getCookieParams();
$files = $request->getFileParams();
$body = new Stream();
$stream->write(json_encode(['foo' => 'bar']));
$response = (new Response())
->withStatus(200, 'OK!')
->withHeader('Accept', 'application/json')
->withBody($stream);
$status = $response->getStatusCode();
$reason = $response->getReasonPhrase();
$contentType = $response->getHeader('Content-Type');
$data = json_decode((string) $response->getBody());
More specifically, start thinking in terms of middleware:
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface 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);
}
}