Parsing a GET request in PHP with an Azure Function

While playing around with PHP (experimental support) in Azure Functions, I noticed that there is no documentation yet and very few examples, so here’s my first simple example on how to build an Azure Function using PHP to parse a very simple GET request.

I’m assuming you’ve set up your function, go into Files and edit the function.json file:

 

{
  "bindings": [
    {
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get"
      ],
      "authLevel": "function"
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    }
  ],
  "disabled": false
}

This sets the function to listen to get requests and ignore the default Azure Table storage stuff.

Then open the run.php file and add some code. Azure Functions expose the original HTTP data in the form of PHP Environment variables. POST requests come in the form of files you’ll have to open and parse, but GET requests are very straightforward:

 

<?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"]; 

?>

Et voila, you’ve got your first Azure Function working with a PHP GET request:

And you can probably guess what I intend to use this for 😉

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Paul Bond
Paul Bond
3 years ago

Hey, this is great! How would you get the output in a HTTP response rather than just the function Log though?

So for example if I sent the following and wanted it to echo my name back to me:

https://[[functionapp]].azurewebsites.net/api/%5B%5Bfunction%5D%5D?code=%5B%5Bcode%5D%5D&name=paul