Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REST API - Javascript or PHP? [closed]

With a REST API I can receive a response in XML or in JSON. This can be done using PHP or Javascript (using jQuery) for example.

I want to know the advantages and the disadvantages of the different languages. This is what I figured out so far:

  • PHP seems easier than JavaScript when data needs to be used on the server side for later.
  • JavaScript runs on the client side and does not make a load on the server when fetching data with external URLs
like image 789
Jens Törnell Avatar asked Feb 24 '23 13:02

Jens Törnell


2 Answers

The Javascript call will not place a load on your server if the REST API is on an external domain (i.e. not yours). jQuery's ajax() call offers a work-around to allow you to GET data from external domains.

Use PHP if:

  • You want to save the output from the API in your own database
  • You want to call the API perdioically to get updates rather than have each user call it every time they view a page that uses it. If you have thousands of pageviews a day but the data from the API only changes once a month then this would save expensive calls.
  • If you need to POST to the API. You can't do a POST to another domain using Javascript
  • You want to do some heavy analysis on the data or you want to analysis data from multiple API calls over time

Use Javascript when:

  • The API provides up-to-the-minute data that need to be queried on each page view
  • You are using Ajax to update your webpages
like image 200
Steve Claridge Avatar answered Feb 26 '23 03:02

Steve Claridge


Disregarding any other server side language, when you create your own REST API, the most common way is using PHP for backend and JavaScript for the client side. But there is also the possibility to write JavaScript at the backend (Learning Server-Side JavaScript with Node.js).

Javascripts run on the client side and does not make a load on the server when fetching data with external URLs.

Thats only half the truth, if I understood your question right. If you need data from an external source, JavaScript will be prevented from doing so due to the same origin policy. But there are many possibilities to load data from some other origin like ajax proxy (using your backend as bridge) or JSONP.

like image 37
Sascha Galley Avatar answered Feb 26 '23 03:02

Sascha Galley