KISS PARSE GOODBYE

Bits and Bytes: Forget Parse, Use Azure Mobile Services to Create an API for your Mobile App or Website

When you are building a mobile app or a website mostly likely you are going to need some type of data repository. This can be either files, video or in the form of a “database”. In order to access these repositories you need some sort of access mechanism. Sometimes it is a library or SDK others it is a connection to the database. However, with a mobile app or even websites that are distributed the connection to these repositories can be tricky. You need to create some sort of endpoint for these. Back in the day these were called Web Services now they are called API or Web API’s. Creating them can be time consuming as well. You have to create all of the routines needed to get and put data. You will need to create authentication mechanisms to make sure the proper user is getting and putting data. All in all this could be very time consuming.

Until today! Enter in Azure Mobile Services. A quick and easy way to consume an API endpoint for iOS, Android, Windows and HTML/JavaScript. We will look at creating these API’s using the “Classic Azure Portal”.

channel-9-mobile-services

If you want to use the new method of Azure App Services, here is a video as well.

channel-9-mobile-app-services

First off, make sure you sign up for your Azure account before we begin.

 

Create Mobile Service

  1. Login to the Classic Azure Portal. Click on NEW -> MOBILE SERVICE -> CREATE
  2. Enter a url for your clients to access the service.
  3. Choose eiher an existing SQL Server or Create a New SQL Server (this is used to host your database).
  4. Done
Responsive image

Create Your Data Table(s)

  1. Click on the Mobile Service name you just created.
  2. In the navigation tab, select DATA.
  3. On the bottom of the screen, click CREATE and choose a table name. You can also modify permissions if needed.
  4. Done
Responsive image

Modify Your Schema

  1. Click on the table name you just created and select COLUMN from the navigation tab.
  2. On the bottom of the screen, click ADD COLUMN and add as needed.
  3. You can connect to the Database you just created by using a SQL client tool and your connection string.
  4. To find your connection string click on DB in the left nav, find your Database name and you will see your connection string.
  5. Windows: can use SQL Management Tools or Cloud Explorer (within Visual Studio), or Mac: SQLPro for MSSQL or Mac SQL Studio
  6. FYI: if you are connecting to the SQL server you will have to add your ip address to the server firewall.
  7. Done
Responsive image

Access Your Table API

  1. Automatically you will get a CRUD (create, read, update, delete) API created for you.
  2. Click on the table and then click on SCRIPT in the navigation tab
  3. There will be 4 scripts that you can modify if needed.
  4. FYI: if you name your object and properties in your code, then no need to change anything in the api unless you need specific business logic
  5. Your endpoint will be: https://MOBILE SERVICE NAME.azure-mobile.net/tables/TABLE NAME
  6. Done
Responsive image

Create A Custom API

  1. From our Mobile Services page, click on the navigation tab API.
  2. Click CREATE -> and provide your custom API with a name.
  3. Here you can build custom logic to query multiple tables or perform some other routine.
  4. Your endpoint will be: https://MOBILE SERVICE NAME.azure-mobile.net/api/API NAME
  5. Done
Responsive image

View Errors and Logs

  1. From our Mobile Services page, click on the navigation tab LOGS.
  2. All logs will be there.
  3. Done
Responsive image

PHP and Azure Data Services

When we think of PHP we also think of MySQL. The perfect relationship. PHP can provide the logic and MySQL can provide the data storage. There are others like SQLite, Postgres, MariaDB, MongoDB and more. All of these provide great functionality and performance but there are a few services that Azure has that can be deployed quickly and managed really easy. We are going to look into 3 services that Azure utilizes to make deploying and building PHP apps really easy and scalable.

Using Azure Microsoft SQL Server

This is not something new. PHP has supported MS SQL for a while now but why not utilize the ability of creating a SQL DB out on Azure and using the ODBC drivers to connect to SQL. By using SQL as a Service you can build software-as-a-service (SaaS) applications that use Azure SQL Database to provide flexibility to support both explosive growth. For workloads with unpredictable database resource consumption, the elastic database model gives you the ability to pool resources to use among a group of databases. Instead of overprovisioning a virtual machine to meet peak demand, you can use an elastic database pool to let hundreds or thousands of databases use resources within a budget that you control.

You are also removing virtually all infrastructure maintenance with SQL Database, which provides automatic software patching as part of the service. Meanwhile, built-in system replicas using the quorum writes technique help deliver inherent data protection, database uptime, and system stability, which means fewer hassles for developers and architects. System replicas are automatically moved to new computers, which are provisioned on the fly as old ones fail.

Now you do not have to focus on the “database server” you can just put your focus on your business and technology.

Here is an example. Using sqlsrv_connect and a MS SQL instance out on Azure, you can be up and running in no time.

Using Azure App (Mobile) Services

Azure App Service is the only cloud service that integrates everything you need to quickly and easily build web and mobile apps for any platform and any device. Built for developers, App Service is a fully managed platform with powerful capabilities such as built-in DevOps, continuous integration with Visual Studio Online and GitHub, staging and production support, and automatic patching. Using Mobile Apps in Azure App Service, it offers a highly scalable, globally available mobile application development platform for Enterprise Developers and System Integrators that brings a rich set of capabilities to mobile developers.

Why Mobile Apps?

Mobile Apps in Azure App Service offers a highly scalable, globally available mobile application development platform for Enterprise Developers and System Integrators that brings a rich set of capabilities to mobile developers. With Mobile Apps you can create API’s in minutes. Customize those API’s to include push notifications, authentication and more.

Here is an example on how to set up a Mobile app as well as integrating the service with a PHP application. There is a Azure JavaScript Library that you can import and use for your app.

Using Azure PHP SDK

The other way to utilize manage storage is via Azure Storage (blobs, files, tables). Azure Storage is massively scalable, so you can store and process hundreds of terabytes of data to support the big data scenarios required by scientific, financial analysis, and media applications. Or you can store the small amounts of data required for a small business website. Wherever your needs fall, you pay only for the data you’re storing. Azure Storage currently stores tens of trillions of unique customer objects, and handles millions of requests per second on average.

Azure Storage is elastic, so you can design applications for a large global audience, and scale those applications as needed – both in terms of the amount of data stored and the number of requests made against it. You pay only for what you use, and only when you use it.

In order to access Azure Storage in PHP you will need to install the Azure PHP SDK and configure your application as well as set up a storage account.

Wrap up

Each of these options will provide you with managed data storage, easy setup, and highly scalable so you can focus on your code and your business.

Check out phpmssqldemo.azurewebsites.net for examples of all three scenarios.