i want wo write a small php program which which fetches a csv file from a remote server via cURL. It works so far and now I want to use this csv file in JS.
What is the best way to give this file from php to JS?
Create tmp file on php server and use a link to this file in JS? Is there a smarter way?
Thanks
I would use AJAX to call the PHP file, which will then use cURL to get the CSV.
If you choose to load the file in the DOM directly with PHP, the page will not appear until the cURL operation has completed. From experience, I noticed that if a page takes 30 seconds to load, it’s better to show the page with a “loading…” than displaying a blank page for 29 seconds and show everything in the last second.
By the way, using cURL and calling with AJAX is also a way to circumvent Same-domain AJAX.
I wouldn’t suggest to use PHP code in the JS, as it would remove the possibility to cache/minify the JS efficiently, if you choose to do so down the road. Also, it’s not pretty.
How I would do it;
Output its data in JSON, its probably going to look like this
“{“1”:{“col1″:”hey”,”col2″:”hey2″},”2″:{“col1″:”heyhey”,”col2″:”heyhey2″}}”
Display data in a table, probably using dataTables or something equivalent.