Supercharge WP REST API with Google App Engine

It seems that I am pretty late here to play with WordPress REST API. It’s really super cool! Now I can think about how well and fast I can distribute or work with wordpress blog posts. Even now you can host your whole WordPress inside a static HTML page. Funny? Yea, like your HTML page will interact with your REST API json response and small JS can help you to display inside page. Then you will get 10000000x super fast blog post delivery. Trust me! It works. You will understand if you are a little techie guy.

I was thinking about if I can easily get all of my posts’s data as REST API Json response then easily I can use them wherever and whenever I want. But there must be a trick. Cause still this REST API is working from inside your main WP hosting.

But what if (IF) we can recruit some middleman who can hold data and deliver those data without even interacting with your original WordPress site. So you will save lots of money and your site’s performance will be super boosted.

I just run an experiment (I shared my experimented snippet here) with Google App Engine. Why Google App Engine? Ask that to google yourself. Google App Engine is the best solutions for ready-set-go types of app deployment with lots of features that Google maintains themselves. So I am going to write a small PHP code which will load your site’s REST API JSON data and after that rest of the request it will server from directly GAE memcache. So, more easily, if your site has 1M audience a day, then most probably you will hit your original WP site once, and 999999 user will get that data from directly App Engine (PHP app data persistent through memcache).

I just tried it and it works and I tried to do 50,000 hits and it just took few milliseconds to deliver me my wordpress site’s data from Google App Engine and I just designed a HTML page with simple JS that will load the content from my App Engine and will display inside HTML page, and you know HTML page you can serve billions user in a minute. Voila 🙂

So here is my little PHP script and also I uploaded the full GAE implementation in a GitHub repository so you can directly download, change your site’s URL and deploy to Google App Engine.

And here is the app.yaml configuration for Google App Engine

This snippets are available on GitHub. If you face any trouble and want to optimize your site and make it super-fast and stable with this types of solutions, reach me anytime at shaharia@previewtechs.com

GeoIP alternative to get IP location – Google App Engine PHP Recipes

Today’s recipes is all about to get very basic information about your visitor’s IP address location. Unfortunately, in Google App Engine, PHP GeoIP extension is not enabled in their standard environment. So sometimes, it’s essential for application developer to get the visitor’s IP address location.

Here, you can easily get user’s IP address location with 3 information easily and by default from Google App Engine. In GAE, if you will discover that Google app engine itself added few extra _X_headers in $_SERVER super-global variable. I am dumping the $_SERVER variable here.

 

And from that, we can easily get the visitor’s City, City-level lat long, country and region that primary will help you to get visitor’s location.

Connect Google Cloud SQL from Local Machine using Unix Socket

Firstly,
You need to install Google Cloud SDK and check if you have gcloud command available in terminal. After installing gcloud you need to configure gcloud sdk by running gcloud init command.

Secondly,
I am assuming, you have configured your Google Cloud SQL instance for your project. If yes, then continue, otherwise you need to read “Google Cloud SQL Quickstarter Guide“.

Now Download cloud_sql_proxy.linux.amd64 package in your local machine with

this command.

Thirdly,
Rename by mv cloud_sql_proxy.linux.amd64 cloud_sql_proxy
and give execution permission by chmod +x cloud_sql_proxy

Fourth,
Create application default credentials using “gcloud auth application-default login” command

Fifth,
Create a directory sudo mkdir -p /cloudsql for Google Cloud SQL proxy unix socket and make that directory writable sudo chmod 777 /cloudsql

Sixth,
start sql socket proxy by ./cloud_sql_proxy -dir=/cloudsql &

At last,
Now you can have working mysql DSN “mysql:unix_socket=/cloudsql/INSTANCE_CONNECTION_NAME” which you can use in your application and your application will work with Google Cloud SQL instance directly from local machine.

Please ask me anything about this post in comment below. I will be glad to assist you happily.