我正在寻找这种情况是否可能 -
- 我的网站托管了几个 mp3 文件。 URL 看起来像 www.abc.com/.mp3。
- 假设我收到来自 iOS/android 用户的传入请求,使用他的浏览器访问我上面的 mp3 链接
我的问题是,我可以收听事件(访问我的网站 mp3 的事件)然后向用户发送另一个 mp3 吗?
我正在考虑使用 PHP 和 javascript 来执行此操作。请指导我执行此操作的方法。
最佳答案
如果您想像问题中所说的那样使用 php 或 javascript,我可以想象在客户端(javascript)或服务器端(php)为您解决问题的两种解决方案:
在客户端,使用 javascript,您可以将对 mp3 的调用包装在 javascript 函数中(可能通过 ajax)。此函数将检查浏览器并根据它获取正确的 mp3
在服务器端,使用 php,您可以将 mp3 查询包装在 PHP 脚本中,比如 getmp3.php?file=xxx.mp3。您的客户端页面不会直接获取 mp3,而是向此 php 脚本询问。此脚本将检查用户代理并发送 mp3,例如:
// Get the asked mp3 $askedfile = _GET['file']; // Get browser info $browser = get_browser(null); // Put filename to the proper mp3 file depending on the browser $filename = ...; // Put the proper content/type : header('Content-Type: audio/mp3'); header('Content-Disposition: attachment; filename="$askedfile"'); header('Content-Length: '.filesize($file)); // Send the mp3 readfile($filename);
关于php - 使用 php 和/javascript 监听网站上的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14848883/