Example

Now we will create an Azure Function with the DNS lookup example in PHP:

  1. Log into Azure Portal and click on Azure Function App. We already created Azure Function App in the previous chapter, so now we will create the Azure Function.
  2. Click on the + sign and then select the coding language, as shown in the following screenshot:

By default, PHP gives the template that fetches the entities from a storage table when it receives the HTTP request.

The code for fetching a data from the storage table is already there in PHP. We can directly use it. However, in this example, we will not use the storage table. We will write code for DNS lookup.

This allows us to create only the HTTP GET request:

  1. Now, give a name to the function, provide the storage account connection, and click on the Create button.
  2. Even if we are not using the storage connection, we still need to provide a storage account connection because until now we don't have much of an option for PHP. PHP code is in experimental mode, so in the future this will not be the case:
  1. By default, it will give the following template:

We can directly write this code. Add some entity in the storage table and run this code. We will get all the entity stored in the storage table. This is the HTTP get request, so we use the function URL and hit that URL in the browser to check the response:

  1. Now we will modify the code, as shown in the following screenshot:
  1. Let's understand the code first, then we will run and test the HTTP APIs:
  1. In the preceding code, gethostbyname() is a predefined function in PHP, which takes the name of the host and returns its IP address. gethostbyaddr() is also a predefined function, which takes the IP address as input and returns the name of the host:
  1. The next line of the code prints the array response, as shown in the following screenshot:
  1. Finally, the response is set for the HTTP request:
  1. Now, we will test our HTTP request API. For that, get the function URL first. Click on </> Get function URL, as shown in the following screenshot:
  1. Once we click on </> Get function URL, we will see a pop-up with the URL. Click on the Copy button to copy the URL:
  1. Now, open any browser to check out the API. We will use Chrome to check our API, as shown in the following screenshot:
  1. Now we have created the PHP Azure Function. We can try with a different hostname and IP address.
  2. Since we don't have much of an option available in Azure Function, this is a simple example to start with PHP Azure Function.
  3. Try to explore more with this example by taking the parameter from the query string and then providing the response accordingly.
  4. Since it is in experimental mode, it only provides us the HTTP GET request. Request body won't work here because currently it doesn't allow the POST request.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.39.60