What's wrong with this PHP Twitter API POST?

Is there any meaningful difference between accessing an API (Twitter for example) with a server side language like PHP or client side with Javascript?

  • Just for fun, I'm building a basic web page to display calls to the Twitter API and I've been given conflicting advice as to whether or not to interface with the API in PHP or Javascript. I'm an utter amateur, but for the life of me, I can't think of any meaningful reason why it should matter.  Or is it something that would matter more in a more serious, larger scale app?

  • Answer:

    You are absolutely right - for most cases, it does not matter if you were accessing the API's via frontend or backend. These are some of the reasons why I would choose do it either ways - Reasons to choose server side language (PHP, for example) If I am not comfortable telling the world what my API access key is, I'd choose to access them server side. Client side code makes it visible to everyone. Some API's want you to send a few HTTP request headers. Most of the times it is needed to have an extra layer of validation. If I were dealing with one of these API's, I'd be left with no choice but to code server side as client side JS doesn't let you set request headers for JSONP requests. If I have my own business logic on top of the API data, I'd choose to access these API's server side. E.g. let's say you were building a "search application" which collates results from Google (web search), Google News (news search), Technorati (blog search), IMDB (video search) and Flickr (photo search). You intend to offer a single search box to your end user and order the results by relevance - which in your case is, let's say, recency. The first thing that would come naturally to you is to model a search result - basically, to create a business object first. This object would be universal and independent of the API source you are getting the data from. So, in this case, I'd convert all the responses to MySearchObject which will contain uniqueId, title, pic, description, dateOfPublishing etc. I'd code my controllers and frontend based on this data object. If I need better logging and persistence, I'd choose to access the API server side. Most server side languages have built in logging mechanisms. It is much easier to track stuff like "how many times the API is being accessed", "what are the popular queries through the API on your site" etc. Moreover, if I needed to persist some (or complete) data from the API in my own database, I'd prefer to do it server side. Some API's allow you to persist data and you may choose to do it for multiple reasons like not going back to the API for the same user session and same user query. Reasons to choose client side Javascript If none of the reasons mentioned above are a deterrent for my use case, I'd use JS. Most's API, these days, return data in JSON. So, once you have the data via these API's, it is tad easy to use it in Javascript. Be it manipulating the DOM to simply render the data or to perform some sort of computing on top of it - client side JS is capable of doing both with grace. Add to this the availability of frameworks like jQuery which make it all the more easier.

Avlesh Singh at Quora Visit the source

Was this solution helpful to you?

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.