最近研究了函数计算,很不错的服务,总结一个php存在的一个问题,为以后的小白们避免同样的错误。
<?php
use RingCentral\Psr7\Response;
function initializer($context) {
echo 'initializing' . PHP_EOL;
}
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['status'] = 0;
//如何不存在$queries['id']
//bug无法判断不存在的变量
if($queries['id']){
$params['status'] = 1;
}
$respHeaders = array('Content-Type' => 'application/json');
$respBody = json_encode($params);
return new Response(200, $respHeaders, $respBody);
}
解决方案:
$GLOBALS['queries'] = $queries;
$id = isset($GLOBALS['queries']['
id'])?$GLOBALS['queries']['id']:'';
if($id){
$params['status'] = 1;
}
利用$GLOBALS来解决,isset判断