PHP 的 Azure 函数

Azure Functions with PHP

我正在使用 PHP 试用 Azure Functions。 获取请求信息对我不起作用。

我根本找不到任何文档,其中包含有关如何通过 PHP 代码使用 Azure Functions 的信息。

根据仅有的几个示例,似乎为了检索输入信息,您需要首先获取 req 变量(或您在函数配置中分配的任何名称)的内容。 那就是包含请求信息的文件的路径(理论上)。

$input_path = getenv('req');

到目前为止,如果我检查它的内容,我会得到如下信息:

D:\local\Temp\Functions\Binding\e2b6e195-02f7-481b-a279-eef6f82bc7b4\req

如果我检查文件是否存在,结果为真,但文件大小为 0。

有谁知道在这里做什么?有人有例子吗?有谁知道文档在哪里?

谢谢

好的,不幸的是,正如您所发现的,php 的文档非常有限。

目前看代码可能是最好的文档。这是 InitializeHttpRequestEnvironmentVariables 函数,它将请求元数据添加到脚本语言(节点、powershell、php、python)的环境中。

重要的环境变量是:

  • REQ_ORIGINAL_URL
  • REQ_METHOD
  • REQ_QUERY
  • REQ_QUERY_<queryname>
  • REQ_HEADERS_<headername>
  • REQ_PARAMS_<paramname>

我假设您发出了 GET 请求,在这种情况下没有内容(req 是一个空文件),但是您会看到这些其他环境变量包含请求数据。如果你要用正文发出 POST 请求,那么 req 会有数据。

这里是使用 Azure 函数解析 PHP 中的 GET 请求的完整示例:)

https://www.lieben.nu/liebensraum/2017/08/parsing-a-get-request-in-php-with-an-azure-function/

来自来源的片段:

<?php 

//retrieve original GET string 
$getReqString = getenv('REQ_QUERY'); 

//remove the ? for the parse_str function 
$getReqString = substr($getReqString,1,strlen($getReqString)); 

//convert the GET string to an array 
$parsedRequest = array(); 
parse_str($getReqString,$parsedRequest); 

//show contents of the new array
print_r($parsedRequest); 

//show the value of a GET variable 
echo $parsedRequest["code"]; 

?>