PHP Classes

File: class_mixcloud_player.php

Recommend this page to a friend!
  Classes of Lukas Bonsch   PHP Mixcloud Player   class_mixcloud_player.php   Download  
File: class_mixcloud_player.php
Role: Class source
Content type: text/plain
Description: the class
Class: PHP Mixcloud Player
Embed player of music hosted in Mixcloud
Author: By
Last change: some cleanups i missed
Date: 1 year ago
Size: 8,544 bytes
 

Contents

Class file image Download
<?PHP
/**
 * simple mixcloud api reader
 * gets the uploads from a given user or given user and playlist
 * returns the embed code for webpages
 * Important note: this class always returns an iframe with the mixcloud player. There is no way to access the audio stream in any other way
 * @author maestro300
 * @version 2.0
 * @copyright Copyright (c) 2015 1.0, 2017 1.5 , 2022 2.0 Lukas maestro300 B.
 * @license http://opensource.org/licenses/GPL-3.0 GNU Public License
 */
class mixcloud_player
{
   
    private
$user;
    private
$plslug;
    private
$api;
    private
$dark;
    private
$hidecover;
    private
$playlist;
   
/**
     * initial settings for the mixcloud api
     * @param string $mixclouduser mixcloudusername
     */
   
function __construct($mixclouduser) {
       
$this->user = $mixclouduser;
       
$this->Api_Obj();
       
$this->dark = 1;
       
$this->hidecover = 1;
    }
   
   
/**
     * generates the api object from mixcloud json
     */
   
private function Api_Obj($what = "", $titel = "")
    {
       
$titel = strtolower(preg_replace('/[^A-Za-z0-9\-]/', '', str_replace(' ', '-', $titel)));
        switch(
$what)
        {
           case
"show": $mixcloud = json_decode(file_get_contents("http://api.mixcloud.com/".$this->user."/".$titel."/")); break;
           case
"playlist": $mixcloud = json_decode(file_get_contents("http://api.mixcloud.com/".$this->user."/playlists/".$titel."/cloudcasts/")); break;
           case
"cloudcasts": $mixcloud = json_decode(file_get_contents("http://api.mixcloud.com/".$this->user."/cloudcasts/")); break;
           default:
$mixcloud = json_decode(file_get_contents("http://api.mixcloud.com/".$this->user."/")); break;
        }
       
$this->api = $mixcloud;
    }
   
   
/**
     * sets the player style for the object
     * mixcloud currently supports a light and dark player
     * also the option hidecover can determin how the cover is shown in the player
     * @param boolean [$hidecover = true] sets the width of the iframe - defaut: true
     * @param boolean [$dark = false] sets the height of the iframe - default: false
     * @return string html player
     */
   
function setPlayerstyle($dark = false, $hidecover = true)
    {
           
$this->dark = (int)!$dark;
           
$this->hidecover = (int)$hidecover;
    }

   
/**
     * get's the latest mixcloud upload (this the first entry in the mixcloud api obj);
     * @param string [$witdh = "100%"] sets the width of the iframe - defaut: 100%
     * @param string [$height= "100%"] sets the height of the iframe - default: 120
     * @return string html player
     */
   
function getLatest_Player($witdh = "100%", $height = "120")
    {
       
$this->api_obj("cloudcasts");
        if(isset(
$this->playlist))
        {
           
$this->api_obj("playlist", $this->playlist);
        }
       
$player = json_decode(file_get_contents(str_replace("www", "api", $this->api->data[0]->url."embed-json/")));
        return
str_replace(array('hide_cover=1', 'light=1', 'width="100%"', 'height="120"'), array('hide_cover='.$this->hidecover, 'light='.$this->dark, 'width="'.$witdh.'"', 'height="'.$height.'"'), $player->html);
         
    }
                  
   
/**
     * legacy function
     * get's the latest mixcloud upload (this the first entry in the mixcloud api obj);
     * @param int $offse between 1 and x for the offset (1 = latest mixcloud upload, 2 = second latest upload etc)
     * @return string html player
     */
   
function get_latest_player($witdh = "100%", $height = "120")
    {
        return
$this->getLatest_Player($witdh, $height);
    }
   
   
/**
     * get's a specifig show by title
     * @param string showname
     * @param string [$witdh = "100%"] sets the width of the iframe - defaut: 100%
     * @param string [$height= "100%"] sets the height of the iframe - default: 120
     * @return string html player
     */
   
function getShow($showtitle, $witdh = "100%", $height = "120")
    {
       
$this->Api_Obj("show", $showtitle);
       
$player = json_decode(file_get_contents(str_replace("www", "api", $this->api->url."embed-json/")));
        return
str_replace(array('hide_cover=1', 'light=1', 'width="100%"', 'height="120"'), array('hide_cover='.$this->hidecover, 'light='.$this->dark, 'width="'.$witdh.'"', 'height="'.$height.'"'), $player->html);
    }
   
   
/**
     * gets all cloudcast as php object
     * @return object
     */
   
function getCloudcastobj()
    {
       
$this->Api_Obj("cloudcasts");
        return
$this->api;
    }
   
   
/**
     * get Playlist object
     * @return object
     */
   
function getPlaylist_Obj()
    {
       
$this->api_obj("playlist", $this->playlist);
        return
$this->api;
    }
   
   
/**
     * sets the Playlist by title
     */
   
function setPlaylist($playlisttitel)
    {
       
$this->playlist = $playlisttitel;
    }
                    
   
/**
     * returns a specific player
     * @param int $offse between 1 and x for the offset (1 = latest mixcloud upload, 2 = second latest upload etc)
     * @return string html player
     */
   
function getPlayer($offset = 1, $witdh = "100%", $height = "120")
    {
       
$this->api_obj("cloudcasts");
        if(isset(
$this->playlist))
        {
           
$this->api_obj("playlist", $this->playlist);
        }
       
        if(
$offset-1 > count($this->api->data))
        {
           throw new
Exception('Out of Range - Please use integer > 0 and < '.count($this->api->data));
        }
        else
        {
           
$player = json_decode(file_get_contents(str_replace("www", "api", $this->api->data[$offset-1]->url."embed-json/")));
return
str_replace(array('hide_cover=1', 'light=1', 'width="100%"', 'height="120"'), array('hide_cover='.$this->hidecover, 'light='.$this->dark, 'width="'.$witdh.'"', 'height="'.$height.'"'), $player->html);
        }
    }
   
   
/**
     * legacy function
     * returns a specific player
     * @param int $offse between 1 and x for the offset (1 = latest mixcloud upload, 2 = second latest upload etc)
     * @return string html player
     */
   
function get_player($offset = 1, $witdh = "100%", $height = "120")
    {
        return
$this->getPlayer($offset, $witdh, $height);
    }
   
    function
getUser()
    {
       
$this->api_obj();
        return
$this->api;
    }
   
    function
get_user()
    {
        return
$this->getUser()
    }
   
    function
getUser_HTML()
    {
       
$this->api_obj();
       
$html = '<div style="box-sizing: border-box; background-color:#FCFCFC; border:1px solid #D7D7D7; height:120px; color:#555555">';
       
$html .= '<img src="'.$this->api->pictures->large.'" style="float:left; height:120px; margin-right:10px;">';
       
$html .= '<h2 style="margin:2px;">'.$this->api->username.'</h2>';
       
$html .= '<p>'.$this->api->biog.'</p>';
       
$html .= 'City: '.$this->api->city;
       
$html .= ' - Country: '.$this->api->country;
       
$html .= ' - Favorite Count: '.$this->api->favorite_count;
       
$html .= ' - Listen Count '.$this->api->listen_count;
       
$html .= ' - Cloudcasts: '.$this->api->cloudcast_count;
       
$html .= '</div>';
        return
$html;
    }
                     
                  
                  
   
/**
     * returns the count of the available cloudcast as int
     * return int
     */
   
function getCount()
    {
       
$this->api_obj("cloudcasts");
        if(isset(
$this->playlist))
        {
           
$this->api_obj("playlist", $this->playlist);
        }
        return
count($this->api->data) + 1;
    }
   
   
/**
     * legacy function
     * the count of the available cloudcast as int
     * return int
     */
   
function get_count()
    {
        return
$this->getCount();
    }
                  
   
/**
     * get's the latest mixcloud upload (this the first entry in the mixcloud api obj);
     * @return string html player
     */
   
function __toString()
    {
        return
$this->getLatest_Player();
    }
   
   
/**
     * returns the mixcloud api obj
     * @return object the mixcloud json obj
     */
   
function getApi_Obj()
    {
        return
$this->api;
    }
}
?>