-1

我的问题令人沮丧,我正在从 iTunes 的 API 中获取封面。这是获取歌曲的艺术家和名称并检索艺术品的代码:

$str = $song[0];
$artist_name = $song[0];
$posted = preg_replace('/ -.*/', '', $artist_name);
$manual_referer = 'http://itunes.com/';
$itunes_song = str_replace("'", '', $str);
$args = array(
    'term' => $itunes_song,
    'entity' => 'song',
    'limit' => '1',
);

echo $song_itunes;
$url = "https://itunes.apple.com/search?";
foreach ($args as $key => $val) {
    $url .= $key . '=' . rawurlencode($val) . '&';
}

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $manual_referer);
$body = curl_exec($ch);
curl_close($ch);

$json = json_decode($body, true);
$results = $json['results'];

foreach ($results as $result) {
    $res = $result['artworkUrl100'];
}

现在,我的问题是:假设这首歌的名字是:T Pain - 5 O'Clock,因为系统对单引号的歌曲有问题,我做了这两行:

$itunes_song = str_replace("'", '', $str);

'term' => $itunes_song,

但是 $itunes_song 的输出是: T Pain - 5 O'Clock当它需要是T Pain - 5 OClock(没有单引号)。

我不知道我在这里做错了什么,有人可以帮助我吗?:(

完整代码:

Dachi, the full script:

<?php
include('config_radio.php');

$scfp = fsockopen("$scip", $scport, &$errno, &$errstr, 30);
if (!$scfp) {
    $scsuccs = 1;
    echo '' . $scdef . ' is Offline';
}
if ($scsuccs != 1) {
    fputs($scfp, "GET /admin.cgi?pass=$scpass&mode=viewxml HTTP/1.0\r\nUser-Agent: SHOUTcast Song Status (Mozilla Compatible)\r\n\r\n");
    while (!feof($scfp)) {
        $page .= fgets($scfp, 1000);
    }

    //define  xml elements
    $loop = array(
        "STREAMSTATUS", "BITRATE", "SERVERTITLE", "CURRENTLISTENERS",
        "MAXLISTENERS", "BITRATE"
    );
    $y = 0;
    while ($loop[$y] != '') {
        $pageed = ereg_replace(".*<$loop[$y]>", "", $page);
        $scphp = strtolower($loop[$y]);
        $$scphp = ereg_replace("</$loop[$y]>.*", "", $pageed);
        if ($loop[$y] == SERVERGENRE || $loop[$y] == SERVERTITLE || $loop[$y] == SONGTITLE || $loop[$y] == SERVERTITLE)
            $$scphp = urldecode($$scphp);

        // uncomment the next line to see all variables
        //echo'$'.$scphp.' = '.$$scphp.'<br>';
        $y++;
    }
    //end intro xml elements
    ######################################################################################################################
    ######################################################################################################################
    /////////////////////////part 2\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
    //get song info and history
    $pageed = ereg_replace(".*<SONGHISTORY>", "", $page);
    $pageed = ereg_replace("<SONGHISTORY>.*", "", $pageed);
    $songatime = explode("<SONG>", $pageed);
    $r = 1;
    while ($songatime[$r] != "") {
        $t = $r - 1;
        $playedat[$t] = ereg_replace(".*<PLAYEDAT>", "", $songatime[$r]);
        $playedat[$t] = ereg_replace("</PLAYEDAT>.*", "", $playedat[$t]);
        $song[$t] = ereg_replace(".*<TITLE>", "", $songatime[$r]);
        $song[$t] = ereg_replace("</TITLE>.*", "", $song[$t]);
        $song[$t] = urldecode($song[$t]);
        $dj[$t] = ereg_replace(".*<SERVERTITLE>", "", $page);
        $dj[$t] = ereg_replace("</SERVERTITLE>.*", "", $pageed);
        $r++;
    }
    //end song info
    fclose($scfp);
}

//display stats
if ($streamstatus == "1") {
    //you may edit the html below, make sure to keep variable intact

    $str = $song[0];
    $artist_name = $song[0];
    $posted = preg_replace('/ -.*/', '', $artist_name);
    $manual_referer = 'http://itunes.com/';
    $itunes_song = str_replace("'", '', $str);
    $args = array(
        'term' => $str,
        'entity' => 'song',
        'limit' => '1',
    );

    $url = "https://itunes.apple.com/search?";
    foreach ($args as $key => $val) {
        $url .= $key . '=' . rawurlencode($val) . '&';
    }

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_REFERER, $manual_referer);
    $body = curl_exec($ch);
    curl_close($ch);

    $json = json_decode($body, true);
    $results = $json['results'];

    foreach ($results as $result) {
        $res = $result['artworkUrl100'];
    }

    $str_fixed = str_replace('-', '</div><div style="width: 1px; height: 16px;"></div><div style="font-size: 16px; font-weight: bold; margin-left: 10px; float: left; color: #FFF;">', $str);

    if ($posted == "sweeper" || $posted == "DRY") {
        echo '
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1255" />
    <meta http-equiv="refresh" content="15" />
    </head>
    <span style="float: right; color: #FFF; font-size: 16px; direction: rtl; font-weight: bold; margin-top: 10px;">פרסומות וחוזרים!</span>';
    } else if (strlen($res) == 0) {
        echo '
    <p align="center"><img src="media/images/nopic.jpg" style="max-width: 100px; max-height: 100px;" align="left">
    <b></b> <div style="font-size: 14px; margin-left: 10px; color: #FFF; float: left;">' . $str_fixed . '</div></p><b>
    ';
    } else {
        echo '
    <p align="center"><img src="' . $res . '" style="max-width: 100px; max-height: 100px;" align="left">
    <b></b> <div style="font-size: 14px; margin-left: 10px; color: #FFF; float: left;">' . $str_fixed . '</div></p><b>
    ';
    }
    echo '
    </p>
    </body>

    </html>';
}
if ($streamstatus == "0") {
    echo '
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1255" />
    <meta http-equiv="refresh" content="15" />
    </head>
    <span style="float: right; color: #FFF; font-size: 16px; direction: rtl; font-weight: bold; margin-top: 10px;">אין שידורים כעת!</span>';
}
?>
4

1 回答 1

0

您的代码应该没问题,您可以检查str_replace 此处的行为

这只是我的假设,您在检查时引用了错误的变量

$itunes_song = str_replace("'", '', $str);
$args = array(
    'term' => $itunes_song,
    'entity' => 'song',
    'limit' => '1',
);

echo $song_itunes;

我想$song_itunes应该在$itunes_song那里。

我的另一个问题是,$str当您需要使用新创建$itunes_song的例如这里时,您仍在使用:

$str_fixed = str_replace('-', '</div><div style="width: 1px; height: 16px;"></div><div style="font-size: 16px; font-weight: bold; margin-left: 10px; float: left; color: #FFF;">', $str);

或在这里

$args = array(
    'term' => $str,
    'entity' => 'song',
    'limit' => '1',
);
于 2013-09-01T06:34:51.353 回答