PHP 使用 iTunes API 生成预览链接 - 慢

标签 php html mysql itunes

我遇到了问题。我正在尝试使用 iTunes 搜索 API 生成一个预览歌曲按钮来预览存储在 MySQL 数据库中的歌曲。

我编写了以下代码:

<?php

// Script de conexión a la base de datos.
include "includes/connect.php";

$songs = "";

$query = $db->prepare("SELECT artista, titulo, nombre, count(id) as uploadCount FROM canciones GROUP BY artista, titulo ORDER BY uploadCount DESC");
$query->execute(array($songs));
$songs = $query->fetchAll(PDO::FETCH_ASSOC);

echo '<table id="tblCanciones" width="95%" border="0" align="center" cellpadding="0">
            <tr align="left">
              <th class="tblCancionesHeader" width="33%">Artista</th>
              <th class="tblCancionesHeader" width="32%">T&iacute;tulo</th>
              <th class="tblCancionesHeader" width="20%">Pedida por</th>
              <th class="tblCancionesHeader" width="10%">Veces</th>
              <th class="tblCancionesHeader" width="4%"></th>
            </tr>';

foreach($songs as $song) {

// Buscar el link de la preview de la canción en la iTunes store...
$term = urlencode($song["artista"])."+".urlencode($song["titulo"]);
$json =  file_get_contents('http://itunes.apple.com/search?term='.$term.'&limit=1&entity=musicArtist,musicTrack,album,mix,song');
$array = json_decode($json, true);

        echo '<tr align="left">
                        <td nowrap="nowrap" class="tblCancionesFila">'.$song["artista"].'</td>
                        <td nowrap="nowrap" class="tblCancionesFila">'.$song["titulo"].'</td>
                        <td nowrap="nowrap" class="tblCancionesFila">'.$song["nombre"].'</td>
                        <td nowrap="nowrap" class="tblCancionesFila">'.$song["uploadCount"].'</td>';

                        if (!isset($array['results'][0]['previewUrl'])) {
                            echo '<td nowrap="nowrap" class="tblCancionesFila">---</td>';
                        } else {
                            echo '<td nowrap="nowrap" class="tblCancionesFila"><a href="'.$array['results'][0]['previewUrl'].'"><img src="images/imgPreview.jpg" width="20" height="20" alt="preview" title="preview" border="0" /></a></td>';
                        }

        echo '</tr>';

}

echo '</table>';

?>

它的工作或多或少有一些限制。我只是想让它获取预览链接,所以我将结果限制为 1。所以有时它找不到结果...

主要问题是获取结果很慢,非常慢......

我想我做错了什么。

是否有任何其他简单的方法来获得相同的功能,但可能使用 iTunes 以外的其他服务,例如 Spotify、SoundCloud 或其他服务?

谢谢!

最佳答案

万一有人发现这有用......我找到了一种更明智的方法!

我刚刚改变了问题的焦点,所以现在,我不是在 SQL 查询中生成链接,这显然非常慢而且不太明智......,而是每次用户发送新歌曲时生成一个链接到 SQL 数据库。

因此,用户发送一首歌曲(艺术家、标题),就在将数据插入数据库之前,我使用 itunes API 生成包含给定艺术家和歌曲标题的预览链接。然后我将艺术家、标题和预览链接输入到数据库中。

因此,生成包含所有歌曲、标题、艺术家和预览链接的数据表就像从 SQL DB 中提取数据一样简单。

有时思考事情可能非常有用......

关于PHP 使用 iTunes API 生成预览链接 - 慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23324861/

相关文章:

php - 使用 php volley 库从服务器获取大量 json 数据花费太多时间

php - 如何从 Doctrine Fixture 引用中获取实体?

php - 查询;单击以显示 html 内容和倒计时,然后显示其他内容

php - 使用 PHP 的登录页面出现问题

mysql - 命令文本属性尚未正确初始化

mysql - 查询无重复项

php - 自动绑定(bind)所有结果

php - 根据纬度/经度下载和存储谷歌街景全景图

html - 针对不同平台调整图像大小

javascript - 更改图像 map 上的边框