gw php,apigw.php

function main_handler($event, $context) {

// $_SERVER = [];

// $_SERVER['IS_SCF'] = true;

// if(is_file(__DIR__ . "/wsConfig.json")) {

// $wsConfig = json_decode(file_get_contents(__DIR__ . '/wsConfig.json'), 1);

// foreach ($wsConfig as $k=>$v){

// $_SERVER[$k] = $v;

// }

// unset($wsConfig);

// }

/*

|--------------------------------------------------------------------------

| Run The Application

|--------------------------------------------------------------------------

|

| Once we have the application, we can handle the incoming request

| through the kernel, and send the associated response back to

| the client's browser allowing them to enjoy the creative

| and wonderful application we have prepared for them.

|

*/

if ($event->path == "/"){

$event->path = '/index.html';

}

if ($event->path == "/agent/"){

$event->path = '/agent/index.html';

}

if ($event->path == "/manage/"){

$event->path = '/manage/index.html';

}

// 处理js, css文件

if (preg_match('#\.html.*|\.js.*|\.css.*|\.html.*#', $event->path)) {

$filename = "/var/user/public" . $event->path;

echo $filename;

$handle = fopen($filename, "r");

$contents = fread($handle, filesize($filename));

// echo $contents;

fclose($handle);

$headers = [

'Content-Type' => '',

'Cache-Control' => "max-age=8640000",

'Accept-Ranges' => 'bytes',

];

return array(

'isBase64Encoded' => false,

'statusCode' => 200,

'headers' => $headers,

'body' => $contents

);

}

// // 处理图片

if (preg_match('#\.gif.*|\.jpg.*|\.png.*|\.jepg.*|\.swf.*|\.bmp.*|\.ico.*#', $event->path)) {

$filename = "/var/user/public" . $event->path;

echo $filename;

$handle = fopen($filename, "r");

$contents = fread($handle, filesize($filename));

// echo $contents;

fclose($handle);

$headers = [

'Content-Type' => '',

'Cache-Control' => "max-age=86400",

];

return array(

"isBase64Encoded" => true,

"statusCode"=> 200,

"headers"=> $headers,

"body"=> base64_encode($contents),

);

}

// 处理php文件

system("mkdir -p /tmp/cache");

system("mkdir -p /tmp/framework/sessions");

system("mkdir -p /tmp/framework/cache");

system("mkdir -p /tmp/framework/views");

system("chmod -R 755 /tmp");

system("/var/lang/php7/bin/php -v");

//laravel框架启动

require __DIR__.'/bootstrap/autoload.php';

$app = require __DIR__.'/bootstrap/app.php';

//过滤OPTIONS请求方式

if($event->httpMethod=='OPTIONS'){

$headers = [

'Content-Type' => 'application/json;charset=utf-8',

'Access-Control-Allow-Origin' => '*',

'Access-Control-Allow-Credentials' => 'true',

'Access-Control-Allow-Headers' => 'x-requested-with,content-type',

];

$content = ['code'=> '99','msg'=>'xcnzxmn','data'=>''];

return array(

'isBase64Encoded' => false,

'statusCode' => 200,

'headers' => $headers,

'body' => json_encode($content)

);

}

//处理定时任务

if(isset($event->Type) && $event->Type == "Timer"){ // 定时触发器

$path = '/admin/timingUpdateFormId'; // 路由

$event->httpMethod = 'POST';

$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);

$request = \Illuminate\Http\Request::create($path,$event->httpMethod,[], [], [], []);

$response = $kernel->handle($request);

$content = $response->getContent();

$headers = [

'Content-Type' => 'application/json;charset=utf-8',

'Access-Control-Allow-Origin' => '*',

'Access-Control-Allow-Credentials' => 'true',

'Access-Control-Allow-Headers' => 'x-requested-with,content-type',

];

return array(

'isBase64Encoded' => false,

'statusCode' => 200,

'headers' => $headers,

'body' => $content

);

}

//获取提交信息

$req = '';

$res = '';

if($event->httpMethod == 'GET'){

$req = $event->queryString;

$req = json_encode($req);

}else if($event->httpMethod == 'POST'){

$req = $event->body;

$res = $event->queryString; //当post请求URL带参数时处理

$res = json_encode($res);

}

//去除{}在判断是否为空

$new = str_replace("{","",$req);

$new_req = str_replace("}","",$new);

//获取headers头信息

$array = get_object_vars($event->headers);

//解析提交的参数

$data = [];

if($new_req){

$data = !empty($req) ? json_decode($req, true) : [];

if(!$data){

$data = parseData($req,$array); //解析form-data数据

}

}

if($res){

$info = !empty($res) ? json_decode($res, true) : [];

$data = array_merge($data,$info);

}

$event->path = str_replace("//", "/", $event->path);

var_dump($event->path);

$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);

$request = \Illuminate\Http\Request::create($event->path,$event->httpMethod,$data, [], [], []);

$response = $kernel->handle(

$request

);

$content = $response->getContent();

//获取返回结果

$result = json_decode($content,true);

//规定code=3为下载信息

if ($result['code'] == 3){

$filename=$result['data'];

$handle=fopen($filename,'r');

$re=fread($handle,filesize($filename));

return array(

'isBase64Encoded' => true,

'statusCode' => 200,

'headers' => array(

"Content-type" => "application/octet-stream",

"Content-Disposition" => "attachment; filename=".$result['name'],

'Accept-Ranges' => 'bytes',

),

'body' => base64_encode($re),

);

}

$headers = [

'Content-Type' => 'application/json;charset=utf-8',

'Access-Control-Allow-Origin' => '*',

'Access-Control-Allow-Credentials' => 'true',

'Access-Control-Allow-Headers' => 'x-requested-with,content-type',

];

return array(

'isBase64Encoded' => false,

'statusCode' => 200,

'headers' => $headers,

'body' => $content

);

return $response;

}

//form-data参数解析为数组

function parseData($data,$array){

$formDataTag = "multipart/form-data; boundary=";

$key = "--" . str_replace($formDataTag, "", $array['content-type']);

$params = preg_split("/[\\s]*${key}-*[\\s]*/", $data);

array_pop($params);

array_shift($params);

$requestParam = [];

foreach ($params as $str){

$strArr = explode("\r\n\r\n", $str);

if(count($strArr) < 2){

$strArr = explode("\n\n", $str);

}

$string = preg_replace("/[\\s\\S]*Content-Disposition: form-data; name=\"([^\"]+)\"[\\s\\S]*/", "$1", $strArr);

if ($string[0] === 'file') {

// 文件类型

$requestParam[$string[0]] = $string[1];

} else {

$requestParam[$string[0]] = $string[1];

}

}

return $requestParam;

}

?>

一键复制

编辑

Web IDE

原始数据

按行查看

历史

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值