2014-04-27 6 views
0

문제가 있습니다. MySQL 데이터베이스에 저장된 노래를 iTunes 미리보기 API를 사용하여 미리보기 할 수있는 미리보기 노래 버튼을 생성하려고합니다.PHP가 iTunes API로 미리보기 링크를 생성합니다 - SLOW

<?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로 제한하고 싶습니다. 그래서 때때로 결과를 찾지 못합니다 ...

주요 문제는 결과를 가져 오는 데 시간이 오래 걸리며 느립니다. ..

내가 뭔가 잘못하고있는 것 같아.

동일한 기능을 얻는 다른 쉬운 방법이 있을지 모르지만 spotify, soundcloud 또는 기타 같은 itunes 이외의 다른 서비스를 사용하고 있습니까?

감사합니다.

+0

어떻게 PHP를 사용 아이튠즈에서 모든 게임 (응용 프로그램) 정보를 얻을 방법을 가르쳐 주 시겠어요? 나는 찾고 있지만 대답을 얻을 수 없다. 나는 또한 검색 API를 사용 해요. 그러나 나는 그것을 어떻게 사용하는지 몰랐다. 여기 내 질문은 http://stackoverflow.com/questions/23696311/get-game-details-from-itunes-using-php#_=_입니다. –

답변

0

누구든지이 기능을 유용하게 사용할 수 있습니다 ...이 작업을 수행하는 데 더 현명한 방법을 찾았습니다!

방금 ​​내 문제의 초점을 변경 했으므로 분명히 매우 느리고 현명하지 않은 SQL 쿼리 내부의 링크를 생성하는 대신 ... 사용자가 새로운 링크를 보낼 때마다 링크를 생성합니다. 노래를 SQL 데이터베이스에 저장합니다.

따라서, 사용자가 노래 (작가, 제목)를 전송하고 바로 DB에 데이터를 삽입하기 전에, 나는 노래의 주어진 아티스트 및 제목과 미리보기 링크를 생성하는 아이튠즈 API를 사용합니다. 그런 다음 아티스트, 타이틀 및 미리보기 링크를 DB에 입력합니다.

그래서 모든 노래, 제목, 아티스트 및 미리보기 링크 데이터 테이블을 생성하는 SQL DB에서 데이터를 추출하는 것만 큼 쉽습니다.

가끔은 ... 상황이 매우 유용 할 수 있습니다 생각

관련 문제