Google App Engine app.yaml PHP scripting parameters

十年热恋 提交于 2019-12-31 02:56:05

问题


In my GAE PHP app.yaml i am trying to do this:

application: myapp
version: 1
runtime: php
api_version: 1
threadsafe: yes

handlers:

- url: /sitemap.xml    
  static_files: sitemap.xml
  upload: /sitemap\.xml

- url: /MyOneLink
  script: /myDynamicContent.php?myparam=hardcoded_value_1

- url: /MySecondLink
  script: /myDynamicContent.php?myparam=hardcoded_value_2

so one can browse http://example.com/MyOneLink and get the result of the dynamic php (which depends of the hardcoded myparam value)

the problem is that when browsing, nothing is shown. any idea ?

btw: you can figure out why i am also publishing a "sitemap.xml": it will be used to expose all myLinks

thanks diego


回答1:


The other answers would be fine for a finite number of values that are hardcoded (as shown in question).

But if you want to work with a truly dynamic version with infinite possibilities of values, you may think of the following (does not work):

- url: /MyLinks/(.*)/?
  script: /myDynamicContent.php?myparam=\1

The above does not work. You can workaround the problem by using a simple PHP hack.

Update the app.yaml to:

- url: /MyLinks/.*
  script: /myDynamicContent.php

In myDynamicContent.php, get the value of $_SERVER['REQUEST_URI'] and parse this string to get the intended value for myparam.

Update! More elegant method:

<?php
$requestURI = parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
$requestURI = explode("/", $requestURI);
$myparam = $requestURI[2];
echo $myparam;
?>

Since parse_url always gets the path info, we can safely depend on hardcoded indices. The array produced by explode for string /MyLinks/value_1 will contain an empty string at index 0, MyLinks at 1, value_1 at 2, and so on.

Original clunkier method:

<?php
$requestURI = explode("/", $_SERVER["REQUEST_URI"]);
for ($i = 0; $i < count($requestURI); $i++) {
    if (strcmp($requestURI[$i], "MyLinks") == 0) {
        $myparam = $requestURI[$i + 1];
        break;
    }
}
echo $myparam;
?>

Tip: You can use single quotes ' instead of double quotes "




回答2:


You cannot pass parameters in the "script:" parameter.

One way to fix this would be two have two "entry" scripts, which then include your main script, like this:

<?php
$_GET['myparam'] = 'hardcoded_value_1';
require('main_script.php');

Which you can then reference in app.yaml

This is probably the quickest way to make your existing code work (although there are better ways to do it).




回答3:


reading the oficial doc https://cloud.google.com/appengine/docs/php/config/mod_rewrite i did this:

<$php
$path = parse_url($_SERVER['PATH_INFO'], PHP_URL_PATH);

if ($path == '/path') {
}
?>


来源:https://stackoverflow.com/questions/29018756/google-app-engine-app-yaml-php-scripting-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!