I am using a cron job to get the users tweets etc and we are saving it in a text file. Now when the user visits the site we need to show these contents. Which of the following is better?
- Curl or file_get_contents()
I researched a lot on this and found that curl is better over FGC. but all of them are for external domains. What we need is the answer for same domain.
Also can you suggest a better way for doing this? than saving it as a text file and presenting to users? Will it be more effective if we create an extra DB table for these sort of contents?
Always use file_get_contents() for getting contents of a file in filesystem. You do not need to make a HTTP request, since if the file is in your filesystem then you can simply read it there.
For example, if you stored your tweets in a file /my-tweets/user124.txt then you can read it:
It’s faster than cURL, since it doesn’t make any HTTP requests and you don’t have to worry about allow_url_fopen PHP setting either.
Also, regarding fopen() and file_get_contents(), the latter is better for code readability, unless you want to read in the tweet file line after line (in which case fopen() is a better option).