You can use HTTP handlers to efficiently process HTTP requests. When you invoke a function, Function Compute uses the handler that you specify in the function code to process HTTP requests. This topic describes the structure and provides examples of PHP HTTP handlers.

Signatures for HTTP handlers

The following sample code describes a signature for a PHP HTTP handler. You need to only implement one function to respond to HTTP requests.

<?php
use RingCentral\Psr7\Response;

function handler($request, $context): Response{
    /*
    $body       = $request->getBody()->getContents();
    $queries    = $request->getQueryParams();
    $method     = $request->getMethod();
    $headers    = $request->getHeaders();
    $path       = $request->getAttribute("path");
    $requestURI = $request->getAttribute("requestURI");
    $clientIP   = $request->getAttribute("clientIP");
    */
    return new Response(
        200,
        array(
            'custom_header1' => 'v1',
            'custom_header2' => ['v2', 'v3'],
            'Content-Type' => 'text/plain',
        ),
        'hello world'
    );
}    
The following content describes the code snippets in the sample code:
  • handler: the name of the HTTP handler.
  • $request: the HTTP request structure.
  • Response: the HTTP response structure.
  • $context: the context information. For more information, see Context.

HTTP request structure

The $request parameter follows the PHP Standards Recommendation (PSR) standards on HTTP message interfaces. For more information about HTTP message interfaces, see PSR-7-http-message. For more information about coding standards, see PSR HTTP Message.

The following sample code shows the information carried by the $request parameter:

<?php
    $queries = $request->getQueryParams();
    $method = $request->getMethod();
    $headers = $request->getHeaders();
    $path = $request->getAttribute("path");
    $requestURI = $request->getAttribute("requestURI");
    $clientIP = $request->getAttribute("clientIP");
    $body = $request->getBody()->getContents();
FieldTypeDescription
$headersArrayThe key-value pairs that are sent by an HTTP client. The values are an array and follow the PSR-7 standard.
$pathStringThe path of the HTTP URL.
$queriesArrayThe key-value pairs of the query parameters in the HTTP URL. The values can be a string or an array.
$methodStringThe HTTP method.
$clientIPStringThe IP address of the HTTP client.
$requestURIStringThe URL that excludes the hostname.
$bodyStringThe body of the HTTP request.
Note The key parameter in the key-value pairs is ignored if the key parameter contains one of the following fields or starts with x-fc-. Therefore, you cannot customize the key parameter.
  • connection
  • keep-alive

HTTP response structure

The $request parameter follows the PSR standards on HTTP message interfaces. The following code shows the structure constructor of HTTP responses.

<?php
/**
     * @param int    $status  Status code for the response, if any.
     * @param array  $headers Headers for the response, if any.
     * @param mixed  $body    Stream body.
     */
    public function __construct(
        $status = 200,
        array $headers = array(),
        $body = null,
    )
    {
       //...
    }
Note The $body field can be a string or stream. If you use a stream-type body, you must implement the StreamInterface API in the PSR-7-http-message standard.

Example

The following sample code provides an example on how to use $request and $Response in HTTP functions.

<?php
use RingCentral\Psr7\Response;
function handler($request, $context): Response{
    $body = $request->getBody()->getContents();
    $queries = $request->getQueryParams();
    $method = $request->getMethod();
    $headers = $request->getHeaders();
    $path = $request->getAttribute("path");
    $requestURI = $request->getAttribute("requestURI");
    $clientIP = $request->getAttribute("clientIP");

    $params = array(
        'method' => $method,
        'clientIP' => $clientIP,
        'requestURI' => $requestURI,
        'path' => $path,
        'queriesMap' => $queries,
        'headersMap' => $headers,
        'body' => $body,
    );

    $respHeaders = array('Content-Type' => 'application/json');
    $respBody = json_encode($params);
    return new Response(200, $respHeaders, $respBody);
}  

Limits

  • Request limits

    If a request exceeds one of the following limits, the system returns status code 400 and error InvalidArgument.

    FieldDescriptionHTTP status codeError code
    headersThe total size of the keys and values in the request headers cannot exceed 4 KB. 400InvalidArgument
    pathThe total size of the request path and query parameters cannot exceed 4 KB.
    bodyThe total size of the body of a synchronous invocation request cannot exceed 32 MB. The total size of the body of an asynchronous invocation request cannot exceed 128 KB.
  • Response limits

    If a response exceeds one of the following limits, the system returns status code 502 and error BadResponse.

    FieldDescriptionHTTP status codeError code
    headersThe total size of the keys and values in the response headers cannot exceed 4 KB. 502BadResponse

References

For more information about the PHP runtime environment, see Overview.