您可以使用HTTP Handler更方便地处理HTTP请求。当调用函数时,函数计算使用您提供的执行方法来处理HTTP请求。本文介绍了PHP HTTP请求处理程序的结构和使用示例等。
HTTP Handler签名
PHP的HTTP Handler签名如下。您只需实现一个函数,就能响应HTTP请求。
<?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'
    );
}    示例解析如下:
- handler:HTTP Handler名称。
- $request:HTTP请求结构体。
- Response:HTTP返回结构体。
- $context:上下文信息。具体信息,请参见上下文。
HTTP请求结构体
$request参数遵循PSR(HTTP message interfaces)标准。更多信息,请参见PSR-7-http-message。具体的代码定义遵循PSR Http Message。
$request参数携带的可用信息代码示例如下:
<?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();| 参数 | 类型 | 描述 | 
| $headers | Array | 存放来自HTTP客户端的键值对,键值对中的值为数组类型,遵循PSR-7标准。 | 
| $path | String | HTTP URL中的路径。 | 
| $queries | Array | 存放来自HTTP URL中的查询部分的键值对,键值对中的值可以是字符串或数组。 | 
| $method | String | HTTP方法。 | 
| $clientIP | String | HTTP客户端的IP地址。 | 
| $requestURI | String | 请求中除host以外的URL。 | 
| $body | String | HTTP请求中的请求体数据。 | 
Headers键值对中的key中包含以下字段或以x-fc-开头的key均会被忽略,因此,不支持自定义。
- connection 
- keep-alive 
HTTP响应结构体
$request参数遵循PSR(HTTP message interfaces)标准。以下代码为HTTP响应结构体构造函数。
<?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,
    )
    {
       //...
    }$body可以是字符串,也可以是Stream。如果使用Stream格式,必须要实现PSR-7-http-message标准中的StreamInterface API接口。
PHP HTTP函数示例
下文代码示例演示了如何使用HTTP函数中的$request和$Response。
<?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);
}  限制说明
- 请求限制 - 如果超过以下限制,会返回400状态码和InvalidArgument错误码。 - 字段 - 限制说明 - HTTP状态码 - 错误码 - headers - 请求头中的所有键和值的总大小不能超过8 KB。 - 400 - InvalidArgument - path - 请求路径以及所有查询参数的总大小不能超过4 KB。 - body - 同步调用请求的Body的总大小不能超过32 MB,异步调用请求的Body的总大小不能超过128 KB。 
- 响应限制 - 如果超过以下限制,会返回502状态码和BadResponse错误码。 - 字段 - 限制说明 - HTTP状态码 - 错误码 - headers - 响应头中的所有键和值对的大小不能超过8 KB。 - 502 - BadResponse 
更多信息
PHP运行环境的详细信息,请参见环境说明。