All Products
Search
Document Center

Function Compute:HTTP handlers

Last Updated:Feb 21, 2024

You can use HTTP handlers to efficiently process HTTP requests. When a function is invoked, 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 items describe the fields 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 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();

Field

Type

Description

$headers

Array

The key-value pairs that are sent by an HTTP client. The values are an array and follow the PSR-7 standard.

$path

String

The path of the HTTP URL.

$queries

Array

The key-value pairs of the query parameters in the HTTP URL. The values can be a string or an array.

$method

String

The HTTP method.

$clientIP

String

The IP address of the HTTP client.

$requestURI

String

The URL that excludes the hostname.

$body

String

The 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.

    Field

    Description

    HTTP status code

    Error code

    headers

    The total size of the keys and values in the request headers cannot exceed 8 KB.

    400

    InvalidArgument

    path

    The total size of the request path and query parameters cannot exceed 4 KB.

    body

    The 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.

    Field

    Description

    HTTP status code

    Error code

    headers

    The total size of the keys and values in the response headers cannot exceed 8 KB.

    502

    BadResponse

References

For more information about PHP runtimes, see Overview.