When you use PHP for programming in Function Compute, you must define a PHP function as the handler function. This topic describes the structure and features of PHP HTTP functions.

Definition

The following sample code defines a basic PHP HTTP handler function.

<? 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"],
            "Set-Cookie" => urlencode("test php") . '=' . urlencode('test;more')
        ),
        "hello world"
    );
}        
Note We recommend that you construct all response headers into the parameters of the response objects, such as parameters in the preceding example. Do not use methods alone in which header can be changed, for example, header and setcookie.

$context parameter

The $context parameter is the same as the $context parameter in the event functions. For more information, see The $context parameter.

$request parameter

The $request parameter follows the PHP Standards Recommendation (PSR) standards on HTTP message interfaces. For more information, 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();        
Parameter Type Description
$headers Array Stores a key-value pair from an HTTP client that sends the HTTP request. The value field is an array and follows the PSR-7 standard.
$path String Specifies the path of the HTTP URL.
$queries Array Stores the key-value pair of the queries parameters in the HTTP URL. The parameter value 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 Function Compute provides system fields by default. Custom fields are not supported. You cannot use the following system fields in the key part of your request headers: accept-encoding, connection, keep-alive, proxy-authorization, te, trailer, transfer-encoding and fields started with x-fc-.

$response parameter

The $response parameter follows the PSR standards on HTTP message interfaces. The constructor of a response is as follows:

<? 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 shows how to use $request and $Response in HTTP functions.

use RingCentral\Psr7\Response;
function php_http_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 any one of the following limits, the system returns status code 400 and error code InvalidArgument.

    Field Limit HTTP status code Error code
    headers The total size of the keys and values in the request header cannot exceed 4 KB. 400 InvalidArgument
    path The total size of the request path and query parameters cannot exceed 4 KB.
    body The body of the HTTP request cannot exceed 6 MB.
  • Response limits

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

    Field Limit HTTP status code Error code
    headers The total size of the keys and values in the response header cannot exceed 4 KB. 502 BadResponse
    body The body of the HTTP response cannot exceed 6 MB.