Ateev Chopra
Ateev Chopra

Reputation: 1026

Many HTTP requests (API) Vs Everything in single php

I need some advice on website design.

Lets take example of twitter for my question. Lets say I am making twitter. Now on the home_page.php ,I need both, Data about tweets (Tweet id , who tweeted , tweet time etc. etc) and Data about the user( userId , username , user profile pic).

Now to display all this, I have two option in mind..

1) Making separate php files like tweets.php and userDetails.php. By using AJAX queries, I can get the data on the home_page.php.

2) Adding all the php code (connecting to db, fetching data ) in the home_page.php itself.

In option one, I need to make many HTTP requests, which (i think) will be load to the network. So it might slow down the website.

But option two, I will have a defined REST API. Which will be good of adding more features in the future.

Please give me some advice on picking the best. Also I am still a learner, so if there are more options of implementing this, please share.

Upvotes: 0

Views: 126

Answers (3)

Kjell
Kjell

Reputation: 832

Adding to Kiee's answer:

It can also depend on the size of your content. If your tweets and user info is very large, the response the single PHP file will take considerable time to prepare and deliver. Then you should go for a "minimal viable response" (i.e. last 10 tweets + 10 most popular users, or similar).

But what you definitely will have to do: create an API to bring your page to life. No matter which approach you will use...

Upvotes: 1

Oswald
Oswald

Reputation: 31675

Don't think of PHP applications as a collection of PHP files that map to different URLs. A single PHP file should handle all your requests and include functionality as needed.

In network programming, it's usually good to minimize the number of network requests, because each request introduces an overhead beyond the time it takes for the raw data to be transmitted (due to protocol-specific information being transmitted and the time it takes to establish a connection for example).

Don't rely on JavaScript. JavaScript can be used for usability enhancements, but must not be used to provide essential functionality of your application.

Upvotes: 1

Kiee
Kiee

Reputation: 10771

In number 1 you're reliant on java-script which doesn't follow progressive enhancement or graceful degradation; if a user doesn't have JS they will see zero content which is obviously bad.

Split your code into manageable php files to make it easier to read and require them all in one main php file; this wont take any extra http requests because all the includes are done server side and 1 page is sent back.

You can add additional javascript to grab more "tweets" like twitter does, but dont make the main functionality rely on javascript.

Upvotes: 1

Related Questions