3 tips to optimize PHP code

3 tips to optimize PHP code

How I divided my server errors by 40 with PHP code optimization

I have been running several Raspberry Pi based trackers for a while, and I wanted to create a common database of detected aircraft and their positions. I looked for online database solution and how I could connect my Pi to them directly in Python, but that was not convenient.

The usual PHP tips to optimize PHP code…

A quick search on php optimization got me a list of things to do:

… and what really worked for me!

The 3 actions that had a tremendous effect on my API have been around database optimisation: first, I rewrote the few INSERT loops in order to INSERT multiple values at once. This was already much better. Then I checked my databases structures, to make sure I had all the right indexes corresponding to my SELECT queries. And finally, I switched all the heavy ones to InnoDB (I am not sure why OVH use MyISAM as default structure…).

What else can help reduce connection errors?

I also did several actions to reduce connection errors to my API server:
- make sure you don’t have any redirection
- make sure you updated requests and certifi on the Raspberry Pi, specially if you are using a secured connection. It can easily be done by:
pip install — upgrade requests certifi urllib3 idna chardet
- use session if you are calling the API regularly:
api_session = requests.Session()
resp = api_session.get() will replace requests.get()
- set a different timeout for connection and response:
api_session.get(, timeout=(5,10))

A Journey to ADS-B Flight Tracking on Raspberry Pi. https://blog.foxtrotcharlie.ovh https://www.foxtrotcharlie.ovh