SoundcloudcomFactory.php 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace Muzich\CoreBundle\ElementFactory\Site;
  3. use Muzich\CoreBundle\ElementFactory\Site\base\BaseFactory;
  4. use Muzich\CoreBundle\Entity\Element;
  5. use Symfony\Component\DependencyInjection\Container;
  6. /**
  7. *
  8. *
  9. * @author bux
  10. */
  11. class SoundcloudcomFactory extends BaseFactory
  12. {
  13. public function getEmbedCode()
  14. {
  15. $url = str_replace('www.', '', $this->element->getUrl());
  16. $data = $this->getCleanedUrl();
  17. $embed_url = null;
  18. // http://soundcloud.com/matas/sets/library-project
  19. if (preg_match("#^\/[a-zA-Z0-9_-]+\/sets\/[a-zA-Z0-9_-]+#", $data, $chaines))
  20. {
  21. $embed_url = $url;
  22. }
  23. // http://soundcloud.com/matas/anadrakonic-waltz
  24. else if (preg_match("#^\/[a-zA-Z0-9_-]+\/[a-zA-Z0-9_-]+#", $data, $chaines))
  25. {
  26. $embed_url = $url;
  27. }
  28. // Si c'est une recherche, on gère pas !
  29. // /search?q[fulltext]=tatou
  30. // /tracks/search?q%5Bfulltext%5D=EEK+A+MOUSSE&q%5Btype%5D=&q%5Bduration%5D=
  31. if (preg_match("#\/search\?q#", $data, $chaines))
  32. {
  33. $embed_url = null;
  34. }
  35. if ($embed_url)
  36. {
  37. // l'url est valide pour l'api javascript que l'on utilise
  38. $id = md5($url);
  39. $height = $this->container->getParameter('soundcloud_player_height');
  40. $embed =
  41. '<object height="'.$height.'" width="100%" id="embed_'.$id.'" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000">
  42. <param name="movie" value="http://player.soundcloud.com/player.swf?url='.$embed_url.'&amp;enable_api=true&amp;object_id=embed_'.$id.'"></param>
  43. <param name="allowscriptaccess" value="always"></param>
  44. <embed allowscriptaccess="always" height="'.$height.'" src="http://player.soundcloud.com/player.swf?url='.$embed_url.'&amp;enable_api=true&amp;object_id=embed_'.$id.'" type="application/x-shockwave-flash" width="100%" name="embed_'.$id.'"></embed>
  45. </object>
  46. ';
  47. return $embed;
  48. }
  49. return null;
  50. }
  51. public function getThumbnailUrl()
  52. {
  53. $url_object = $this->element->getUrl();
  54. $url = null;
  55. $ch = curl_init('http://api.soundcloud.com/resolve.json?url='.$url_object.'&client_id=39946ea18e3d78d64c0ac95a025794e1');
  56. $options = array(
  57. CURLOPT_RETURNTRANSFER => true,
  58. CURLOPT_HTTPHEADER => array('Content-type: application/json')
  59. );
  60. curl_setopt_array( $ch, $options );
  61. $result = json_decode(curl_exec($ch));
  62. if (isset($result->errors))
  63. {
  64. if (count($result->errors))
  65. {
  66. return null;
  67. }
  68. }
  69. $getjsonurl = $result->location;
  70. $ch = curl_init($getjsonurl);
  71. curl_setopt_array($ch, $options);
  72. $result = json_decode(curl_exec($ch));
  73. $url = $result->artwork_url;
  74. return $url;
  75. }
  76. }