Skip to main content

Aura.Http Request and Response

·856 words·5 mins
Hari KT
Author
Hari KT
Freelancer | Founder of Tripti and Tanvish | Maintainer of Aura for PHP, created by Paul M Jones

The Aura.Http package provide you the tool to build and send request and response.

Instantiation:
#

The easiest way is

1
2
3
<?php

$http = require 'path/to/Aura.Http/scripts/instance.php';

What it gives you is an object of Aura\Http\Manager. If you want to create manually you can look into the instance.php

Building your Response
#

Probably you may not have bothered too much on building the http response either the framework does it for you, or until you need to send the correct response.

To create a proper http response via Aura.Http we need to create a response object.

1
2
3
<?php

$response = $http->newResponse();

Now you have the response object. You can set the header via

1
2
3
<?php

$response->headers->set('Header', 'Value');

If you have an array of headers you can use setAll

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<?php

$response->headers->setAll([
    'Header-One' => 'header one value',
    'Header-Two' => [
        'header two value A',
        'header two value B',
        'header two value C',
    ],
]);

So a basic example of setting header value is

1
2
3
<?php

$response->headers->set('Content-Type', 'text/plain');

Setting Content
#

The header values are for the browser to understand what is coming from server, and how it should render etc.

So we need to set the content. This can be achieved via setContent method.

1
2
3
<?php

$response->setContent('<html><head><title></title></head><body>Hello World!</body></html>');

You can always get the content via calling getContent.

Setting and Getting Cookies
#

Sometimes we may want to set the cookies. You can do it as

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<?php

$response->cookies->set('cookie_name', [
    'value'    => 'cookie value', // cookie value
    'expire'   => time() + 3600,  // expiration time in unix epoch seconds
    'path'     => '/path',        // server path for the cookie
    'domain'   => 'example.com',  // domain for the cookie
    'secure'   => false,          // send by ssl only?
    'httponly' => true,           // send by http/https only?
]);

The array keys mimic the setcookie parameters. If you have an array you can use setAll.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<?php

$response->cookies->setAll([
    'cookie_foo' => [
        'value' => 'value for cookie foo',
    ],
    'cookie_bar' => [
        'value' => 'value for cookie bar',
    ],
]);

You can get a cookie by calling get method on cookies.

1
2
3
<?php

$response->cookies->get('cookie_name');

Setting and Getting Status
#

By default the status code is 200. But at some point of time like the one I explained earlier in Status Code 304, we don’t need to send the whole content. But just the status code.

This is possible via setStatusCode and setStatusText

1
2
3
4
<?php

$response->setStatusCode(304);
$response->setStatusText('Same As It Ever Was');

Sending your response
#

And finally we can send the response back. We can call the send method and pass the response object.

1
2
3
<?php

$http->send($response);

The full source code of example is

1
2
3
4
5
6
7
8
<?php

$http = require 'path/to/Aura.Http/scripts/instance.php';
// send a response
$response = $http->newResponse();
$response->headers->set('Content-Type', 'text/plain');
$response->setContent('<html><head><title></title></head><body>Hello World!</body></html>');
$http->send($response);

From terminal start the server by php -S localhost:8000 example.php and pointintg to localhost:8000 in your browser.

In order to render just Hello World! in the browser the Content-Type we added should be text/html.

We can always change the header status code, content-type etc before we call send() method.

Let us modify the example at Status Code 304

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
<?php

$http = require 'path/to/Aura.Http/scripts/instance.php';
$response = $http->newResponse();
if ( isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && 
    $_SERVER['HTTP_IF_MODIFIED_SINCE'] == 'Tue, 15 Jan 2011 12:00 GMT' ) {
    $response->setStatusCode(304);
} else {
    $response->headers->set('Content-Type', 'text/html');
    $response->headers->set('Last-Modified', 'Tue, 15 Jan 2011 12:00 GMT');
    $response->setContent('<html><head><title></title></head><body>Hello World!</body></html>');
}
$http->send($response);

You would have noticed I have used $_SERVER variable. In Aura.Http, there is no methods to access the global server values. This is because the $_SERVER values are not the exact http requested header. The server modifies the request and we will be only getting the manipulated values if we use $_SERVER, $_GET, $_POST values.

Aura.Http only helps you to build, create, modify response and request.

Creating Http Request
#

We talked about response so far. What does Request actually mean?

Client -> Request something -> Server Responds

So that means we are trying to be a client or a browser, and making the necessary headers and sending to server to get the corresponding response.

We can get all the repos of a user in github via curl.

1
curl -i https://api.github.com/users/pmjones/repos

The same can be achieved via Aura.Http. The Aura.Http provides a means to do the same with PHP. It uses curl if it is available or stream to make this happen.

You need to create a Request object.

1
2
3
<?php

$request = $http->newRequest();

Set the url via setUrl method and send.

1
2
3
4
5
6
7
8
<?php

$request->setUrl('https://api.github.com/users/pmjones/repos');
$stack = $http->send($request);
$repos = json_decode($stack[0]->content);
foreach ($repos as $repo) {
    echo $repo->name . PHP_EOL;
}

There are more things to say. It can do basic authentication, post values etc. Browse the documentation examples, source code, tests, and api.

Source Code : https://github.com/auraphp/Aura.Http

Documentation : http://auraphp.github.com/Aura.Http/version/1.0.0/

API : http://auraphp.github.com/Aura.Http/version/1.0.0/api/

Related

Aura Framework Releases Beta 4

·41 words·1 min
Aura.Framework released Beta4. In this release Aura.Framework has extracted the Hello Word package and renamed it as Aura.Demo. You can get the system as a whole from http://auraphp.github.com/system/downloads/ Read about the release from blog post and join discussion at auraphp group

Our Awesome PHP People

·1175 words·6 mins
Recently github introduces contributions chart. So I peeped into some of the people of our php community. It seems @fabpot is the one who contributes almost every day. Just 21 days without any contribution. {% img center /assets/images/github/fabpot.png 687 299 ‘Contributions by fabpot’ ‘Contributions by fabpot’ %} {% img center /assets/images/github/weierophinney.png 687 299 ‘Contributions by weierophinney’ ‘Contributions by weierophinney’ %} {% img center /assets/images/github/pmjones.png 687 299 ‘Contributions by pmjones’ ‘Contributions by pmjones’ %} {% img center /assets/images/github/willdurand.png 687 299 ‘Contributions by willdurand’ ‘Contributions by willdurand’ %} {% img center /assets/images/github/Seldaek.png 687 299 ‘Contributions by Seldaek’ ‘Contributions by Seldaek’ %} {% img center /assets/images/github/markstory.png 687 299 ‘Contributions by markstory’ ‘Contributions by markstory’ %} {% img center /assets/images/github/taylorotwell.png 687 299 ‘Contributions by taylorotwell’ ‘Contributions by taylorotwell’ %} {% img center /assets/images/github/WanWizard.png 687 299 ‘Contributions by WanWizard’ ‘Contributions by WanWizard’ %} {% img center /assets/images/github/drak.png 687 299 ‘Contributions by drak’ ‘Contributions by drak’ %} {% img center /assets/images/github/nikic.png 687 299 ‘Contributions by nikic’ ‘Contributions by nikic’ %} {% img center /assets/images/github/avalanche123.png 687 299 ‘Contributions by avalanche123’ ‘Contributions by avalanche123’ %} {% img center /assets/images/github/shama.png 687 299 ‘Contributions by shama’ ‘Contributions by shama’ %} {% img center /assets/images/github/vrana.png 687 299 ‘Contributions by vrana’ ‘Contributions by vrana’ %} {% img center /assets/images/github/philsturgeon.png 687 299 ‘Contributions by philsturgeon’ ‘Contributions by philsturgeon’ %} {% img center /assets/images/github/DASPRiD.png 687 299 ‘Contributions by DASPRiD’ ‘Contributions by DASPRiD’ %} {% img center /assets/images/github/mvriel.png 687 299 ‘Contributions by mvriel’ ‘Contributions by mvriel’ %} {% img center /assets/images/github/c9s.png 687 299 ‘Contributions by c9s’ ‘Contributions by c9s’ %} {% img center /assets/images/github/weaverryan.png 687 299 ‘Contributions by weaverryan’ ‘Contributions by weaverryan’ %} {% img center /assets/images/github/bschussek.png 687 299 ‘Contributions by bschussek’ ‘Contributions by bschussek’ %} {% img center /assets/images/github/vicb.png 687 299 ‘Contributions by vicb’ ‘Contributions by nateabele’ %} {% img center /assets/images/github/garak.png 687 299 ‘Contributions by garak’ ‘Contributions by garak’ %} {% img center /assets/images/github/beberlei.png 687 299 ‘Contributions by beberlei’ ‘Contributions by beberlei’ %} {% img center /assets/images/github/codeguy.png 687 299 ‘Contributions by codeguy’ ‘Contributions by codeguy’ %} {% img center /assets/images/github/lsmith77.png 687 299 ‘Contributions by lsmith77’ ‘Contributions by lsmith77’ %} {% img center /assets/images/github/stof.png 687 299 ‘Contributions by stof’ ‘Contributions by stof’ %} {% img center /assets/images/github/EavnDotPro.png 687 299 ‘Contributions by EavnDotPro’ ‘Contributions by EavnDotPro’ %} {% img center /assets/images/github/nateabele.png 687 299 ‘Contributions by nateabele’ ‘Contributions by nateabele’ %} {% img center /assets/images/github/kriswallsmith.png 687 299 ‘Contributions by kriswallsmith’ ‘Contributions by kriswallsmith’ %} {% img center /assets/images/github/nrk.png 687 299 ‘Contributions by nrk’ ‘Contributions by nrk’ %} {% img center /assets/images/github/naderman.png 687 299 ‘Contributions by naderman’ ‘Contributions by naderman’ %} {% img center /assets/images/github/gwoo.png 687 299 ‘Contributions by gwoo’ ‘Contributions by gwoo’ %} {% img center /assets/images/github/jwage.png 687 299 ‘Contributions by jwage’ ‘Contributions by jwage’ %} {% img center /assets/images/github/chartjes.png 687 299 ‘Contributions by chartjes’ ‘Contributions by chartjes’ %} {% img center /assets/images/github/Tocacar.png 687 299 ‘Contributions by Tocacar’ ‘Contributions by Tocacar’ %} {% img center /assets/images/github/FrenkyNet.png 687 299 ‘Contributions by FrenkyNet’ ‘Contributions by FrenkyNet’ %} {% img center /assets/images/github/till.png 687 299 ‘Contributions by till’ ‘Contributions by till’ %} {% img center /assets/images/github/Ocramius.png 687 299 ‘Contributions by Ocramius’ ‘Contributions by Ocramius’ %} {% img center /assets/images/github/odino.png 687 299 ‘Contributions by odino’ ‘Contributions by odino’ %} {% img center /assets/images/github/josegonzalez.png 687 299 ‘Contributions by josegonzalez’ ‘Contributions by josegonzalez’ %} {% img center /assets/images/github/dereuromark.png 687 299 ‘Contributions by dereuromark’ ‘Contributions by dereuromark’ %} {% img center /assets/images/github/HosipLan.png 687 299 ‘Contributions by HosipLan’ ‘Contributions by HosipLan’ %} {% img center /assets/images/github/alganet.png 687 299 ‘Contributions by alganet’ ‘Contributions by alganet’ %} {% img center /assets/images/github/ezimuel.png 687 299 ‘Contributions by ezimuel’ ‘Contributions by ezimuel’ %} {% img center /assets/images/github/auroraeosrose.png 687 299 ‘Contributions by auroraeosrose’ ‘Contributions by auroraeosrose’ %} {% img center /assets/images/github/enygma.png 687 299 ‘Contributions by enygma’ ‘Contributions by enygma’ %} {% img center /assets/images/github/ralphschindler.png 687 299 ‘Contributions by ralphschindler’ ‘Contributions by ralphschindler’ %} {% img center /assets/images/github/AD7six.png 687 299 ‘Contributions by AD7six’ ‘Contributions by AD7six’ %} {% img center /assets/images/github/ADmad.png 687 299 ‘Contributions by ADmad’ ‘Contributions by ADmad’ %} {% img center /assets/images/github/augustohp.png 687 299 ‘Contributions by augustohp’ ‘Contributions by augustohp’ %} {% img center /assets/images/github/BlaineSch.png 687 299 ‘Contributions by BlaineSch’ ‘Contributions by BlaineSch’ %} {% img center /assets/images/github/alexbilbie.png 687 299 ‘Contributions by alexbilbie’ ‘Contributions by alexbilbie’ %} {% img center /assets/images/github/narfbg.png 687 299 ‘Contributions by narfbg’ ‘Contributions by narfbg’ %} {% img center /assets/images/github/dg.png 687 299 ‘Contributions by dg’ ‘Contributions by dg’ %} {% img center /assets/images/github/schmittjoh.png 687 299 ‘Contributions by schmittjoh’ ‘Contributions by schmittjoh’ %} {% img center /assets/images/github/jschreuder.png 687 299 ‘Contributions by jschreuder’ ‘Contributions by jschreuder’ %} {% img center /assets/images/github/Geczy.png 687 299 ‘Contributions by Geczy’ ‘Contributions by Geczy’ %} {% img center /assets/images/github/dzuelke.png 687 299 ‘Contributions by dzuelke’ ‘Contributions by dzuelke’ %} {% img center /assets/images/github/evert.png 687 299 ‘Contributions by evert’ ‘Contributions by evert’ %} {% img center /assets/images/github/akrabat.png 687 299 ‘Contributions by akrabat’ ‘Contributions by akrabat’ %} {% img center /assets/images/github/seejohnrun.png 687 299 ‘Contributions by seejohnrun’ ‘Contributions by seejohnrun’ %} {% img center /assets/images/github/guilhermeblanco.png 687 299 ‘Contributions by guilhermeblanco’ ‘Contributions by guilhermeblanco’ %} {% img center /assets/images/github/Maks3w.png 687 299 ‘Contributions by Maks3w’ ‘Contributions by Maks3w’ %} {% img center /assets/images/github/kore.png 687 299 ‘Contributions by kore’ ‘Contributions by kore’ %} {% img center /assets/images/github/dragoonis.png 687 299 ‘Contributions by dragoonis’ ‘Contributions by dragoonis’ %} {% img center /assets/images/github/daschl.png 687 299 ‘Contributions by daschl’ ‘Contributions by daschl’ %} {% img center /assets/images/github/wilmoore.png 687 299 ‘Contributions by wilmoore’ ‘Contributions by wilmoore’ %} {% img center /assets/images/github/TheFrozenFire.png 687 299 ‘Contributions by TheFrozenFire’ ‘Contributions by TheFrozenFire’ %} {% img center /assets/images/github/nickl-.png 687 299 ‘Contributions by nickl-’ ‘Contributions by nickl-’ %} {% img center /assets/images/github/lorenzo.png 687 299 ‘Contributions by lorenzo’ ‘Contributions by lorenzo’ %} {% img center /assets/images/github/rchavik.png 687 299 ‘Contributions by rchavik’ ‘Contributions by rchavik’ %} {% img center /assets/images/github/davidpersson.png 687 299 ‘Contributions by davidpersson’ ‘Contributions by davidpersson’ %} {% img center /assets/images/github/henriquemoody.png 687 299 ‘Contributions by henriquemoody’ ‘Contributions by henriquemoody’ %} {% img center /assets/images/github/Freeaqingme.png 687 299 ‘Contributions by Freeaqingme’ ‘Contributions by Freeaqingme’ %} {% img center /assets/images/github/dbu.png 687 299 ‘Contributions by dbu’ ‘Contributions by dbu’ %} {% img center /assets/images/github/dshafik.png 687 299 ‘Contributions by dshafik’ ‘Contributions by dshafik’ %} {% img center /assets/images/github/Vrtak-CZ.png 687 299 ‘Contributions by Vrtak-CZ’ ‘Contributions by Vrtak-CZ’ %} {% img center /assets/images/github/padraic.png 687 299 ‘Contributions by padraic’ ‘Contributions by padraic’ %} {% img center /assets/images/github/saltybeagle.png 687 299 ‘Contributions by saltybeagle’ ‘Contributions by saltybeagle’ %} {% img center /assets/images/github/Hounddog.png 687 299 ‘Contributions by Hounddog’ ‘Contributions by Hounddog’ %} {% img center /assets/images/github/ceeram.png 687 299 ‘Contributions by ceeram’ ‘Contributions by ceeram’ %} {% img center /assets/images/github/jmikola.png 687 299 ‘Contributions by jmikola’ ‘Contributions by jmikola’ %} {% img center /assets/images/github/cweiske.png 687 299 ‘Contributions by cweiske’ ‘Contributions by cweiske’ %} {% img center /assets/images/github/rdohms.png 687 299 ‘Contributions by rdohms’ ‘Contributions by rdohms’ %}

Thoughts on Packages and Components

·562 words·3 mins
Comparing a software X with Y will not make anything worst, but makes it better. But many of them will think it as a promotional stuff. It is not anyones’s problem. Some like to build it that way, some like the other way. So I am not comparing X with Y here. Not talking about package like Guzzle: I am not mentioning building something like Guzzle, which is a framework that includes the tools needed to create a robust web service client.