Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Enrich your website with data from other services

Thursday, March 12th, 2015 by Servage

apiWhen novice web developers are confronted with the process of Application Programming Interfaces, the reaction is often a feeling of being overwhelmed – both with possibilities, but also with complexities. Understanding and utilizing the power of regular script based interactions between web server and database to render a meaningful HTML output is already pressing many hobby coders to the limit of their understanding. Illustrating interactions between multiple servers through APIs is adding to that complexity, and understandably often creating a reaction of confusion or uncertainty how these services interact technically. The purpose of this post is to demystify the API term, and explain in simple terms how a typical API interaction works. It is really not that complicated, and provides great benefits to your website.

PHP is already an API

When you use PHP to communicate with a database or local files, then you are basically already working with an API. On one side you have your PHP script, which can communicate with the database using certain PHP functionality, hence view, add, edit and remove information from the database. The same thing goes for file operations. PHP has built-in functionality to view, create, edit and delete files on the harddrive, which can be used for exporting data, caching etc. Both examples are APIs where PHP gets a workable relationship with the database or filesystem through some functions.

Webservices with APIs

It may seem a little more complex when reading about webservices and their APIs. Usually there will be talk about URL endpoints, request methods, parameters to send, etc. However, they all cover the same principal method. They all rely on the client accessing a specific server, which returns data in a given format. It is nothing more than that.

Working with HTTP requests in PHP

You could start accessing the API URLs via PHP’s own methods like file_get_contents() which can both fetch from a local file and from external URLs. If the response format of the given API is JSON, then you could easily convert it to a PHP array using the json_decode() function.

Getting your own PHP code to work with an API service may require some coding to create the correct requests to the API, and some code to correctly interpret and convert the response data. However, many web services nowadays actually provide ready-made libraries for common programming languages like PHP and Javascript. That means you include the service providers code into your own project, and then you get access to the functions the service provider has built for the language you use. This makes implementing an API very easy, and not more difficult than operating with the regular database connection.

Examples of good APIs

There are numerous APIs available, and you should figure out what partners may provide data to enrich your given project. Below are some ideas on general APIs often used.

  • Facebook- Used to connect your system with Facebook to facilitate common login, content publishing and information exchange.
  • Twitter – Used to connect your system with Twitter for content exchange, etc.
  • Google Places – Used to search for places and get information about them.
  • Flickr Photos – Used to search for photos that may be useful to support your content.
  • PayPal – Used to process and check payments via the PayPal system.

Most websites and service providers have APIs nowadays to give their users access to products in a programmable way. You could even consider to create your own API to let your users use your platform in such a way.

Enrich your website with data from other services, 4.4 out of 5 based on 8 ratings
Categories: Tips & Tricks

Keywords: , ,

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.