http和php的定义,PHP HTTP函数

在函数计算服务使用PHP编程,需要定义一个函数作为入口函数。本文介绍了PHP HTTP函数的结构和特点。

HTTP函数定义

以下代码示例定义了一个基本的PHP 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"],

"Set-Cookie" => urlencode("test php") . '=' . urlencode('test;more')

),

"hello world"

);

}

说明 建议您所有的响应头都放在构造响应对象的参数里面,例如上述示例。不要单独使用能改变header的方法,例如header、setcookie等。

HTTP函数$context参数

$context参数与事件函数中的$context参数相同,详情请参见

HTTP函数$request参数

$request参数遵循PSR(HTTP message interfaces)标准。更多详情请参见

$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的键中不能使用系统定义字段,例如accept-encoding、connection、keep-alive、proxy-authorization、te、trailer、transfer-encoding和以x-fc-开头的字段。

HTTP函数$response参数

$response遵循PSR(HTTP message interfaces)标准。以下代码为Response构造示例。<?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标准中的

PHP HTTP函数示例

下文代码示例演示了如何使用HTTP函数中的$request和$Response。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);

}

限制说明

请求限制

如果超过以下限制,会返回400状态码和InvalidArgument错误码。

字段

限制说明

HTTP状态码

错误码

headers

请求头中的所有键和值的总大小不能超过4 KB。

400

InvalidArgument

path

请求路径以及所有查询参数的总大小不能超过4 KB。

body

HTTP body的总大小不能超过6 MB。

响应限制

如果超过以下限制,会返回502状态码和BadResponse错误码。

字段

限制说明

HTTP状态码

错误码

headers

响应头中的所有键和值对的大小不能超过4 KB。

502

BadResponse

body

HTTP body的大小不能超过6 MB。

更多信息

PHP运行环境的详细信息,请参见

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值