php - Slim Framework - get all headers -
i discovered slim yesterday , it. have run minor issues. here one:
i send out headers jquery ui app slim rest api. not problem @ jquery end- $.ajax provides capability. however, thought write small slim app test out slim's own ability give me access request headers. here app
function indexfunction() { global $app; $headers = $app->request->headers; echo json_encode($headers); } header('content-type:text/plain'); $app = new \slim\slim(); $app->get("/",'indexfunction'); $app->run();
i opened dhc in chrome , fired off request
http://ipaddr/slimrestapi
after adding header xhash = abc123
for measure started fiddler , watched traffic sent out request. fiddler faithfully reported following headers
host: ipaddr connection: keep-alive xhash: abc123 user-agent: mozilla/5.0 (windows nt 6.1) applewebkit/537.36 (khtml, gecko) chrome/39.0.2171.95 safari/537.36 accept: */* accept-encoding: gzip, deflate, sdch accept-language: en-us,en;q=0.8,de;q=0.6,fr;q=0.4
however, results echoed slim empty json object, {}.
am misunderstanding here or there bug in slim? i'd appreciate help.
the headers in slim instance of slim\helper\set can content want all()
function
json_encode($app->request()->headers()->all());
or in full example
$app->get('/', function() use ($app) { echo json_encode($app->request()->headers()->all()); echo $app->request()->headers()->get('xhash'); });
the example shows how can avoid using global
statement. read on subject http://tomnomnom.com/posts/why-global-state-is-the-devil-and-how-to-avoid-using-it
Comments
Post a Comment