我可以在Python的Google App Engine上运行PHP文件吗?

2 投票
1 回答
1106 浏览
提问于 2025-04-18 00:46

首先,我要说明一下,我是刚开始接触应用开发的新人。

我正在用Google App Engine(谷歌应用引擎)和Python环境开发一个网站。在我创建一个简单的HTML表单并决定用PHP来存储数据时,遇到了一些问题。我想知道,在我的应用是用Python运行的情况下,是否可以使用PHP脚本?

我还注意到GAE现在有PHP的运行环境,那我应该用那个吗?我最开始用Python是因为它比较简单,我觉得不需要用PHP。

这是我的main.py文件的内容,如果这有帮助的话:

import os
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template

class MainHandler(webapp.RequestHandler):
  def get (self, q):
    if q is None:
      q = 'index.html'

    path = os.path.join (os.path.dirname (__file__), q)
    self.response.headers ['Content-Type'] = 'text/html'
    self.response.out.write (template.render (path, {}))

def main ():
  application = webapp.WSGIApplication ([('/(.*html)?', MainHandler)], debug=True)
  util.run_wsgi_app (application)

if __name__ == '__main__':
  main ()

1 个回答

3

不,你不能在Python的运行环境中运行PHP脚本。如果你的应用程序更适合用PHP来写,那就可以使用PHP的运行环境。

app.yaml文件中,设置runtime: php,然后创建handlers:,在这里script:的值就是一个PHP脚本:

applciation: helloworld
version: 1
runtime: php
api_version: 1

handlers:
- url: /.*
  script: helloworld.php

然后helloworld.php可以是一个PHP脚本,比如:

<?php
  echo 'Hello, world!';
?>

注意,你可以使用同一个SDK来同时支持Python和PHP。

撰写回答