You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

修复mp3j-player插件main.php中的PHP未定义索引与常量警告

Fixing Undefined Index/Constant Errors in mp3j-player's removeRemoteTracks Function

Let's break down your problem and walk through the best solutions here:

Why You're Seeing These Errors

  1. Notice: Undefined index: url: This happens because the $track arrays in your playlist (created by the newTrack() function) don't have a 'url' key. When you try to access $track['url'], PHP throws a notice because that index doesn't exist.
  2. Warning: Use of undefined constant 'url': This is almost certainly a typo somewhere in your code—maybe a missing quote, like writing $track[url] instead of $track['url']. PHP assumes url is a constant when it's not quoted, and since no such constant exists, it falls back to treating it as the string "url" while warning you about the mistake.
  3. The 480+衍生 errors from Query Monitor are just repeated instances of these two issues firing every time the removeRemoteTracks loop runs over a track.

Your Current Fix: Switching to 'src' or 'filename'

Great news—this isn't just a temporary workaround, it's actually a correct fix!

Looking at your newTrack() function, the array returned includes 'src' and 'filename' (the actual fields storing track URLs/paths), but no 'url' key. The original removeRemoteTracks code was incorrectly referencing a non-existent key. By switching to 'src' or 'filename', you're aligning the code with the actual data structure of the tracks in your plugin.

Since you've confirmed the plugin works normally after this change, this is a solid, long-term solution.

Should You Add a 'url' Key to newTrack()?

This depends on whether other parts of the plugin expect a 'url' key to exist. Here are two scenarios to consider:

Scenario 1: No other code uses 'url'

Stick with your current fix. There's no need to add a redundant 'url' key if nothing else relies on it. This keeps your code clean and aligned with the plugin's existing structure.

Scenario 2: Other parts of the plugin reference $track['url']

If you find other functions in the plugin that expect 'url' to exist, you have two options:

  1. Add 'url' to newTrack() and sync it with 'src':
    Update the newTrack() function to include the key, and make sure whenever a track's 'src' is set, 'url' is updated too (or vice versa):

    function newTrack () {
        return array(
            'url' => '', // Add this line
            'src' => '',
            'filename' => '',
            'formats' => array(),
            'counterpart' => '',
            'title' => '',
            'caption' => '',
            'image' => '',
            'link' => ''
        );
    }
    

    Then, wherever tracks are populated in the plugin, ensure $track['url'] is set to the same value as $track['src'] (or whichever field holds the actual URL).

  2. Add a fallback check in removeRemoteTracks:
    If you don't want to modify newTrack(), you can adjust the function to use 'url' if it exists, otherwise fall back to 'src':

    function removeRemoteTracks ( $playlist ) {
        $filtered = array();
        foreach ( $playlist as $track ) {
            // Get the track URL safely
            $trackUrl = isset($track['url']) ? $track['url'] : $track['src'];
            
            if ( strpos($trackUrl, $this->Rooturl) !== false || 
                 ( strpos($trackUrl, 'http://') === false && strpos($trackUrl, 'https://') === false ) || 
                 ( strpos($this->theSettings['mp3_dir'], "http://") !== false && strpos($trackUrl, $this->theSettings['mp3_dir']) !== false ) ) {
                $filtered[] = $track;
            }
        }
        return $filtered;
    }
    

    This makes the function more resilient in case some tracks have 'url' and others don't.

Final Recommendation

Since your current fix (using 'src' or 'filename') works perfectly and aligns with the plugin's actual data structure, that's the best approach unless you find other code that depends on the 'url' key. This eliminates the errors now and prevents potential issues in future PHP versions where notice-level errors might be treated more strictly.

内容的提问来源于stack exchange,提问作者audioscapist

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 16:12:29