Join Justin Yost for an in-depth discussion in this video Outsource work, part of PHP: Performance Optimization.
- [Instructor] One of the ways that we can improve the performance of our applications, is to outsource work that is not going to be best performed by our own servers. A good example of this might be a need for our application convert photos from one format to another. We're going to add in this ability in to our application now. To do this, we need to first sign up for an API Key through a service called CloudConvert. In your browser, go to the URL CloudConvert.com. There you should see a sign up link, and if you click on that sign up link and fill in a username, a password, and an email address, you'll be able to sign in to the service.
After you sign up for this service, if you view your dashboard you'll see an API Key for this service. We'll use this API Key later on. The free version of this project will let us convert about 25 photos per day. So you can play around with this a bit, but not too much. To use this API, we need to first install a new composer provider package for the API. To do this, open up your terminal application in the root of your project, and run the command composer require cloud convert/cloudconvert-php.
And now we have the API library for use in our code base. To do this, open up a text editor in the root of your project, and find the source folder. Find the controller folder. And then the photos controller. So in our case, we'll add a new public function. Convert. We're going to add it at the bottom of our file. We'll start by adding in a couple of spaces using the enter key, and we'll start adding our public function convert on line 132.
What it's going to do is its going to convert a photo by taking in an ID, calling our database to get the photo, check and ensure that our photo has not been converted, call our CloudConvert API, tell it to convert the photo, wait for it to finish, and then download the photo. And then set the photo variable to our view layer. Now to actually view this page, we need to add in a template. To do that, we'll add in a template at template, photos, and wide in a new file, and this file will be convert.ctp.
The content of this file will come from the rest of our snippets, our .txt file. This html image tag is what will display our converted photo. So save all of this. Go to our terminal application. Run bin/cake server to start our cake PHP server. And now we'll go to our local host port 8765, we'll go to our controller photos, go to convert, in this case we'll convert a photo at random.
I'll convert the photo with ID 5 in our case. And it looks like we have an invalid API Key. Now the reason is is because we didn't actually pass in our real API Key. So recall. We need to go back to CloudConvert. Go to our dashboard. Copy our API Key. In photos controller, paste in our real API Key. Go back and refresh, and there's our photo.
If you're on windows and using WAMP, you may observe an error with this. You may wind up seeing an error called PHP cURL error code 60. In this case, go to stackoverflow.com/q/21114371. Once there, you'll see changes to make to your PHP .ini file, to get it working for you. Now we can verify that our photo is converted by viewing in our console.
Viewing the photo itself. And verifying that the photo is in fact a .png file, and not our original .jpeg. So we could have, if we really wanted, to write a bunch of code to convert these photos manually, internally. However in this case, by outsourcing this work to someone who can do this better, we can both ensure that the work is getting done correctly, as well as also hopefully, off load some amount of work from our own servers and put it on someone else's servers.
Converting photos to a different format might be a simple problem. But there are tons of API's and solutions out there to make your life a little bit easier. Sometimes you might even just build your own internal service to do a task and have it hosted on a separate server. To do that work independent of your core web servers and improve the performance.
- General optimization techniques and tools
- PHP and Xdebug
- Opcode cache
- Optimization in PHP
- Upgrading PHP
- Macro vs. Micro optimization
- HTTP caching
- HTTP compression