首页 > 解决方案 > 如何在 Google App Engine 上运行 php 脚本?

问题描述

我在 Google App Engine 环境中遇到了一个 php 脚本问题。

我尝试使用 ajax 调用调用 php 脚本,但成功回调总是返回未处理的 php 文件(<?php .... ?>

ajax 调用如下所示:

$.ajax({
        type: "POST",
        url: "/contactform-process.php",
        data: "name=" + name + "&email=" + email + "&message=" + message + "&terms=" + terms, 
        success: function(text) {
            console.log('Hello')
            if (text == "success") {
                cformSuccess();
            } else {
                cformError();
                csubmitMSG(false, text);
            }
        }
    });

我假设我必须声明 Google App Engine 环境正在正确处理 php 文件。我当前的 app.yaml:

runtime: php74

handlers:
- url: /
  static_files: static/index.html
  upload: static/index.html

- url: /de
  static_files: static/de/index.html
  upload: static/de/index.html

- url: /en
  static_files: static/en/index.html
  upload: static/de/index.html

- url: /(.*)
  static_files: static/\1
  upload: static/(.*)

- url: /(.+\.php)$
  script: auto

- url: /contactform-process.php
  script: auto

有谁知道缺少什么,所以 php 文件以正确的方式处理?

标签: phpajaxgoogle-app-engine

解决方案


/contactform-process.php匹配的第一个处理程序是/(.*),它是一个静态文件处理程序,因此它只是按原样返回您的 PHP 文件(不执行它)。

您希望它在匹配(.+\.php)$ 之前匹配/(.*),因此您需要在处理程序列表中切换这两个的顺序。


推荐阅读