REST API-Javascript还是PHP? [关闭]

                <div class="grid--cell fl1 lh-lg">
                    <div class="grid--cell fl1 lh-lg">
                        As it currently stands, this question is not a good fit for our Q&amp;A format. We expect answers to be supported by facts, references,   or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question   can be improved and possibly reopened, <a href="/help/reopen-questions">visit the help center</a> for guidance.

                    </div>
                </div>
            </div>
            <div class="grid--cell mb0 mt8">Closed <span title="2011-07-19 11:58:09Z" class="relativetime">8 years ago</span>.</div>
        </div>
    </aside>

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
</div>

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.

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