Google News
logo
Phalcon - Interview Questions
What is HTTP Request (PSR-7) in Phalcon?
Phalcon\Http\Message\Request is an implementation of the PSR-7 HTTP messaging interface as defined by PHP-FIG.
 
This implementation has been created to establish a standard between middleware implementations. Applications often need to send requests to external endpoints. To achieve this you can use the Phalcon\Http\Message\Request object. In return, our application will receive back a response object.
 
NOTE : Phalcon does not restrict you in using a specific HTTP Client. Any PSR-7 compliant client will work with this component so that you can perform your requests.
 
NOTE : In the examples below, $httpClient is the client of your choice which implements PSR-7.
<?php

use Phalcon\Http\Message\Request;
use Phalcon\Http\Message\Uri;

$request = new Request();
$uri     = new Uri('https://api.phalcon.io/companies/1');

$jwtToken = 'abc.def.ghi';

$request = $request
   ->withMethod('POST')
   ->withHeader('Authorization', 'Bearer ' . $jwtToken)
   ->withHeader('Content-Type', 'application/json')
;

$result = $httpClient->send($request);
 
We are creating a new Phalcon\Http\Message\Request object and a new Phalcon\Http\Message\Uri object with the target URL. Following that we define the method (POST) and additional headers that we need to send with our request. The client then sends the request by using the request object.
 
The above example can be implemented by only using the constructor parameters :
<?php

use Phalcon\Http\Message\Request;

$jwtToken = 'abc.def.ghi';

$request = new Request(
    'POST',
    'https://api.phalcon.io/companies/1',
    'php://memory',
    [
        'Authorization' => 'Bearer ' . $jwtToken,
        'Content-Type'  => 'application/json',
    ]
);

$result = $httpClient->send($request);
The Request object created is immutable, meaning it will never change. Any call to methods prefixed with with* will return a clone of the object to maintain immutability, as per the standard.
Advertisement