{"id":2604,"date":"2017-08-25T12:21:46","date_gmt":"2017-08-25T11:21:46","guid":{"rendered":"https:\/\/www.lieben.nu\/liebensraum\/?p=2604"},"modified":"2017-08-25T12:21:46","modified_gmt":"2017-08-25T11:21:46","slug":"parsing-a-get-request-in-php-with-an-azure-function","status":"publish","type":"post","link":"https:\/\/lieben.nu\/liebensraum\/2017\/08\/parsing-a-get-request-in-php-with-an-azure-function\/","title":{"rendered":"Parsing a GET request in PHP with an Azure Function"},"content":{"rendered":"<p>While playing around with PHP (experimental support) in Azure Functions, I noticed that there is no documentation yet and very few examples, so here&#8217;s my first simple example on how to build an Azure Function using PHP to parse a very simple GET request.<\/p>\n<p>I&#8217;m assuming you&#8217;ve set up your function, go into Files and edit the function.json file:<\/p>\n<p><a href=\"https:\/\/www.lieben.nu\/liebensraum\/wp-content\/uploads\/2017\/08\/azure_functions_php_get_example_function_json.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2605\" src=\"https:\/\/www.lieben.nu\/liebensraum\/wp-content\/uploads\/2017\/08\/azure_functions_php_get_example_function_json.png\" alt=\"\" width=\"1274\" height=\"467\" srcset=\"https:\/\/lieben.nu\/liebensraum\/wp-content\/uploads\/2017\/08\/azure_functions_php_get_example_function_json.png 1274w, https:\/\/lieben.nu\/liebensraum\/wp-content\/uploads\/2017\/08\/azure_functions_php_get_example_function_json-300x110.png 300w, https:\/\/lieben.nu\/liebensraum\/wp-content\/uploads\/2017\/08\/azure_functions_php_get_example_function_json-1024x375.png 1024w, https:\/\/lieben.nu\/liebensraum\/wp-content\/uploads\/2017\/08\/azure_functions_php_get_example_function_json-768x282.png 768w\" sizes=\"auto, (max-width: 1274px) 100vw, 1274px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<pre class=\"brush: css; title: ; notranslate\" title=\"\">\n{\n  &quot;bindings&quot;: &#x5B;\n    {\n      &quot;type&quot;: &quot;httpTrigger&quot;,\n      &quot;direction&quot;: &quot;in&quot;,\n      &quot;name&quot;: &quot;req&quot;,\n      &quot;methods&quot;: &#x5B;\n        &quot;get&quot;\n      ],\n      &quot;authLevel&quot;: &quot;function&quot;\n    },\n    {\n      &quot;type&quot;: &quot;http&quot;,\n      &quot;direction&quot;: &quot;out&quot;,\n      &quot;name&quot;: &quot;res&quot;\n    }\n  ],\n  &quot;disabled&quot;: false\n}\n<\/pre>\n<p>This sets the function to listen to get requests and ignore the default Azure Table storage stuff.<\/p>\n<p>Then open the run.php file and <!--more-->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&#8217;ll have to open and parse, but GET requests are very straightforward:<\/p>\n<p><a href=\"https:\/\/www.lieben.nu\/liebensraum\/wp-content\/uploads\/2017\/08\/azure_functions_php_get_example_run.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2606\" src=\"https:\/\/www.lieben.nu\/liebensraum\/wp-content\/uploads\/2017\/08\/azure_functions_php_get_example_run.png\" alt=\"\" width=\"1393\" height=\"492\" srcset=\"https:\/\/lieben.nu\/liebensraum\/wp-content\/uploads\/2017\/08\/azure_functions_php_get_example_run.png 1393w, https:\/\/lieben.nu\/liebensraum\/wp-content\/uploads\/2017\/08\/azure_functions_php_get_example_run-300x106.png 300w, https:\/\/lieben.nu\/liebensraum\/wp-content\/uploads\/2017\/08\/azure_functions_php_get_example_run-1024x362.png 1024w, https:\/\/lieben.nu\/liebensraum\/wp-content\/uploads\/2017\/08\/azure_functions_php_get_example_run-768x271.png 768w\" sizes=\"auto, (max-width: 1393px) 100vw, 1393px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\n&lt;?php \n\n\/\/retrieve original GET string \n$getReqString = getenv('REQ_QUERY'); \n\n\/\/remove the ? for the parse_str function $getReqString = substr($getReqString,1,strlen($getReqString)); \n\n\/\/convert the GET string to an array \n$parsedRequest = array(); parse_str($getReqString,$parsedRequest); \n\n\/\/show contents of the new array\nprint_r($parsedRequest); \n\n\/\/show the value of a GET variable \necho $parsedRequest&#x5B;&quot;code&quot;]; \n\n?&gt;\n<\/pre>\n<p>Et voila, you&#8217;ve got your first Azure Function working with a PHP GET request:<\/p>\n<p><a href=\"https:\/\/www.lieben.nu\/liebensraum\/wp-content\/uploads\/2017\/08\/azure_functions_php_get_example_output.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2611\" src=\"https:\/\/www.lieben.nu\/liebensraum\/wp-content\/uploads\/2017\/08\/azure_functions_php_get_example_output.png\" alt=\"\" width=\"916\" height=\"167\" srcset=\"https:\/\/lieben.nu\/liebensraum\/wp-content\/uploads\/2017\/08\/azure_functions_php_get_example_output.png 916w, https:\/\/lieben.nu\/liebensraum\/wp-content\/uploads\/2017\/08\/azure_functions_php_get_example_output-300x55.png 300w, https:\/\/lieben.nu\/liebensraum\/wp-content\/uploads\/2017\/08\/azure_functions_php_get_example_output-768x140.png 768w\" sizes=\"auto, (max-width: 916px) 100vw, 916px\" \/><\/a><\/p>\n<p>And you can probably guess what I intend to use this for \ud83d\ude09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>While playing around with PHP (experimental support) in Azure Functions, I noticed that there is no documentation yet and very few examples, so here&#8217;s my first simple example on how to build an Azure Function using PHP to parse a very simple GET request. I&#8217;m assuming you&#8217;ve set up your function, go into Files and &hellip; <a href=\"https:\/\/lieben.nu\/liebensraum\/2017\/08\/parsing-a-get-request-in-php-with-an-azure-function\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Parsing a GET request in PHP with an Azure Function<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[5,36],"tags":[],"class_list":["post-2604","post","type-post","status-publish","format-standard","hentry","category-azure","category-php"],"_links":{"self":[{"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/posts\/2604","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/comments?post=2604"}],"version-history":[{"count":0,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/posts\/2604\/revisions"}],"wp:attachment":[{"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/media?parent=2604"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/categories?post=2604"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/tags?post=2604"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}