In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
Cloud function SCF PHP entry pit what, I believe that many inexperienced people are helpless, for this reason this article summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
Since SCF itself uses bootstrap.php to call our entry function, the default is index.main\_handler, which means calling main\_handler() in index.php file, so many places have to be written differently. PHP generally provides web services, so I mainly talk about API gateway with the cloud function SCF.
The main_handler($event, $context) function will pass two parameters. First, these two parameters are object. You need to use-> to access subitems, such as $event->{'headers'}. It is not very convenient. I usually convert them to arrays:
$event = json_decode(json_encode($event), true);
This is convenient, such as $event <$'headers ']<$'host'].
You can print these two parameters and see what's inside.
We can learn a lot of useful things from it, such as:
$_GET = $event['queryString'];$_POST = $event['body'];$_COOKIE = $event['headers']['cookie'];
php program running in SCF, because the browser is submitted to API gateway, not submitted to SCF, these super global variables do not get anything at all, so they should be obtained in this way.
But we found that $event <$'body '] and $event <$'headers']<$'cookie '] are long strings with several values encoded in the url, which is inconvenient to use, so do some small operations:
$postbody = explode("&",$event['body']);foreach ($postbody as $postvalues) { $pos = strpos($postvalues,"="); $_POST[urldecode(substr($postvalues,0,$pos))]=urldecode(substr($postvalues,$pos+1));}$cookiebody = explode("; ",$event['headers']['cookie']);foreach ($cookiebody as $cookievalues) { $pos = strpos($cookievalues,"="); $_COOKIE[urldecode(substr($cookievalues,0,$pos))]=urldecode(substr($cookievalues,$pos+1));}
This makes it easy to use.
In SCF, global variables currently have a pit, that is, the global variables obtained from the last visit will not be cleared this time, so when this visit is made, the values submitted last time may still be in the global variables. This situation exists regardless of whether it is an inherent super-global of php or defined by itself, so pay attention to unset before using it.
In addition to GET, POST and COOKIE, there is also a more important path for the data submitted by users, such as a url: https://cache.yisu.com/upload/information/20210523/355/727923.jpg? foo=bar, in API Gateway,/path/file.jpg will be placed in $event ['path '], but note that if accessed through API Gateway default url, it will contain/functionname, be careful to remove (the following code removes the starting'/'in the path):
$function_name = $context['function_name'];$host_name = $event['headers']['host'];$serviceId = $event['requestContext']['serviceId'];if ( $serviceId === substr($host_name,0,strlen($serviceId)) ) { // using long url of API gateway //When using API Gateway long links $path = substr($event['path'], strlen('/' . $function_name . '/'));} else { // using custom domain //When using custom domain names $path = substr($event['path'], strlen($event['requestContext']['path']=='/'? '/':$event['requestContext']['path']. '/'));}
After obtaining the information submitted by the user, you can handle it yourself. The process is not detailed, just note:
SCF is read-only, only/tmp/directory can be read and written, this tmp directory concurrent instances do not communicate with each other, the instance is destroyed after the end.
After processing, it is necessary to output to the browser. Note that because the dialogue with the browser is the API gateway,
Echo directly in the code, it will only be displayed in the running log, the browser can not see it at all,
so
We need to return what needs to be displayed to API Gateway in main\_handler.
At this time, if you want to return a web page, the API gateway should check "Integrated Response", SCF should return an array with a specific structure, so that the browser will display it normally, otherwise the browser will only see a bunch of strings.
return [ 'isBase64Encoded' => false, 'statusCode' => 200, 'headers' => [ 'Content-Type' => 'text/html' ], 'body' => $html];
where body is the content of the page we want to return, is a string;
headers is to identify the browser, Location or Set-Cookie to be placed inside;
statusCode is the status code, which can be 302 at Location or 404 at certain times;
isBase64 Encoded is used by API Gateway, telling it whether base64 encryption is inside the body.
If you return, the browser will display an HTML page.
But sometimes, we want to give a file to the user to download, in this case, we need to use isBase64Encoded:
$image_data = fread(fopen('logo.png', 'r'), filesize('logo.png'));return [ 'isBase64Encoded' => true, 'statusCode' => 200, 'headers' => [ 'Content-Type' => 'image/png' ], 'body' => base64_encode($image_data)];
This way browsers will get a png file directly, some browsers pop up to download, some open themselves.
The above code has been submitted to the SCF template library: github.com/tencentyun/scf-demo-repo/tree/master/Php7.2-QRcodewithLogo, and I am not stingy with my comments!
After reading the above content, do you have any methods to master the PHP entry pit in SCF? If you still want to learn more skills or want to know more related content, welcome to pay attention to the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.