带有 Firebase 托管的简单 PHP 联系表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35523819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple PHP contact form with Firebase hosting
提问by Richard Ansell
I'm trying to test out if PHP works from my Firebase hosting using the following:
我正在尝试使用以下方法测试 PHP 是否可以从我的 Firebase 托管中运行:
(index.html)
(index.html)
<form action="welcome.php" method="post">
<input type="submit">
</form>
(welcome.php)
(欢迎.php)
<?php
$to = "[email protected]";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: [email protected]";
mail($to,$subject,$txt,$headers);
?>
Every time I try this the browser keeps on attempting to open the PHP file rather than processing it. Is simple PHP enabled on the Firebase server hosting to process a simple form like this? If I can get it to work this way, I will be building the form out correctly including validation etc.
每次我尝试这个时,浏览器都会继续尝试打开 PHP 文件而不是处理它。Firebase 服务器托管上是否启用了简单的 PHP 来处理这样的简单表单?如果我能让它以这种方式工作,我将正确构建表单,包括验证等。
Thanks,
谢谢,
回答by Frank van Puffelen
From the Firebase Hosting site(emphasis mine):
从Firebase 托管站点(重点是我的):
We deliver all of your static content (html, js, images, etc.)over a secure SSL connection and serve it on a CDN.
我们通过安全的 SSL 连接提供您所有的静态内容(html、js、图像等),并在 CDN 上提供。
Firebase Hosting is for hosting static assets. Firebase currently doesn't offer any way to execute your code on Firebase's servers.
Firebase 托管用于托管静态资产。Firebase 目前不提供任何方式在 Firebase 的服务器上执行您的代码。
Update (2018-08-08): You cannow run Node.js/JavaScript code but connecting your Firebase Hosting project to Cloud Functions + Firebase Hosting. But that still won't allow you to run PHP code.
更新 (2018-08-08):您现在可以运行 Node.js/JavaScript 代码,但将您的 Firebase Hosting 项目连接到 Cloud Functions + Firebase Hosting。但这仍然不允许您运行 PHP 代码。
回答by Yadavalli Santosh
As per the latest update firebase has started using Cloud Functions
根据最新更新 firebase 已开始使用Cloud Functions
Cloud Functions for Firebase lets you run mobile backend code that automatically responds to events triggered by Firebase features and HTTPS requests. Your code is stored in Google's cloud and runs in a managed environment. There's no need to manage and scale your own servers.
Cloud Functions for Firebase 可让您运行移动后端代码,自动响应 Firebase 功能和 HTTPS 请求触发的事件。您的代码存储在 Google 的云中并在托管环境中运行。无需管理和扩展您自己的服务器。
For more : https://firebase.google.com/docs/functions/
回答by Martin Zeitler
There is no PHP
but nodeJS
available for server-side scripting ...
没有PHP
但nodeJS
可用于服务器端脚本...
Google Cloud Functionsare written in JavaScript, and execute in a Node.js runtime.
Google Cloud Functions是用 JavaScript 编写的,并在 Node.js 运行时中执行。
Mandrill also supports nodeJSand it features a Webhooks API. Therefore, one can require that node module within these "cloud functions" and "web hooks" ...and then post with a HTML form onto them.
Mandrill 还支持nodeJS并且它具有Webhooks API。因此,可以在这些“云功能”和“网络挂钩”中要求该节点模块……然后将 HTML 表单发布到它们上。
There would need to be a few HTTPcloud functions defined on the Firebase Console, in order to let them subscribe, unsubscribe and manage their subscriptions. One could even generate the HTML markup for the input form with cloud functions and then attach it. As an example, not tested and no guarantee included:
需要在 Firebase 控制台上定义一些HTTP云函数,以便让他们订阅、取消订阅和管理他们的订阅。甚至可以使用云函数为输入表单生成 HTML 标记,然后附加它。例如,未经测试且不保证包括:
const functions = require('firebase-functions');
const mandrill = require('mandrill-api/mandrill');
var client = new mandrill.Mandrill('YOUR_API_KEY');
/* TODO: add the user on Firebase, respond through the API */
exports.user_add = functions.https.onRequest((req, res) => {
});
/* TODO: change subscription settings on Firebase, respond through the API */
exports.user_edit = functions.https.onRequest((req, res) => {
});
/* TODO: remove the user on Firebase, respond through the API */
exports.user_remove = functions.https.onRequest((req, res) => {
});
/* optional: generate the HTML markup of the form, send HTTP response */
exports.markup = functions.https.onRequest((req, res) => {
});
One can bind the events of Firebase Auth, to keep two user databases in in-sync (this is not required for Mandrill, but required for MailChimp - no matter whether using the PHP or nodeJS wrapper):
可以绑定 Firebase Auth的事件,以保持两个用户数据库同步(这对于 Mandrill 不是必需的,但对于 MailChimp 是必需的 - 无论是使用 PHP 还是 nodeJS 包装器):
exports.on_user_create = functions.auth.user().onCreate(event => {
const user = event.data;
});
exports.on_user_delete = functions.auth.user().onDelete(event => {
const user = event.data;
});
Firebase on Websitesexplains it, while there is a Local Emulatorfor Cloud Functions.
网站上的 Firebase对此进行了解释,同时有一个适用于 Cloud Functions的本地模拟器。
回答by Alexey Shevelyov
You can play around with any of these: Angular, Ember, Knockout, React, Node JS. The same thing you PHP code does you can make happen with pretty much any Javascript technologies - just no dynamic language. Also another way to do it is to used an online form providers like Jot Forms or others. You can create and style the form withing you online form account then simply add it to you site. Then when user post it will post to the form. As a result you have a centralized environment not only for you current site but for any others down the road. You can create a web service and post values there - then do whatever you want with them: save them to the database... Otherwords have another server that handles all those things so you can just call it from Firebase hosted sites. Hope that helps
您可以使用以下任何一种:Angular、Ember、Knockout、React、Node JS。您可以使用几乎任何 Javascript 技术实现 PHP 代码所做的相同事情 - 只是没有动态语言。另一种方法是使用在线表单提供程序,如 Jot Forms 或其他。您可以使用在线表单帐户创建表单并为其设置样式,然后只需将其添加到您的站点即可。然后当用户发布它会发布到表单。因此,您不仅为您当前的站点而且为任何其他站点提供了一个集中式环境。您可以创建一个 Web 服务并在那里发布值 - 然后对它们做任何您想做的事情:将它们保存到数据库中……换句话说,有另一台服务器可以处理所有这些事情,因此您可以从 Firebase 托管站点调用它。希望有帮助
PS: I am currently building a product that is a simplified version of Online Forms to be used on Firebase websites. I am planning to have a few people using for now so if you would like you can email me and I will create an account for you to use it. As long as there is no abuse like sending a bunch of emails - you will be fine!
PS:我目前正在构建一个产品,该产品是要在 Firebase 网站上使用的在线表单的简化版本。我现在打算让几个人使用,所以如果你愿意,你可以给我发电子邮件,我会创建一个帐户供你使用。只要没有发送一堆电子邮件之类的滥用 - 你会没事的!