【问题标题】:PHP generate preview link with iTunes API - SLOWPHP 使用 iTunes API 生成预览链接 - 慢
【发布时间】:2014-04-27 14:53:58
【问题描述】:

我遇到了问题。我正在尝试使用 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 或其他?

谢谢!

【问题讨论】:

  • 您能告诉我如何使用 php 从 iTunes 获取所有游戏(应用程序)的详细信息吗?我正在寻找,但我无法得到答案。我也使用过搜索 API。但我不知道如何使用它?这是我的问题stackoverflow.com/questions/23696311/…

标签: php html mysql itunes


【解决方案1】:

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

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

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

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

有时想的东西可能非常有用......

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-04
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 2015-10-29
    • 2018-09-18
    • 1970-01-01
    相关资源
    最近更新 更多