/** * Gets track by url * @param string $url * @return string */ public static function search($query) { // create a client object with your app credentials $client = new Services_Soundcloud(Base::$g['soundcloud_client'], ''); // find all sounds $tracks = $client->get('tracks', array('q' => $query, 'limit' => 40)); $tracks = json_decode($tracks); $ret = array(); foreach ($tracks as $track) { $table = Track::create(); $table->scid = $track->id; $table->name = $track->title; $table->scurl = $track->permalink_url; $table->duration = round($track->duration / 1000); $table->save(); $ret[] = $table->orm->asArray(); } return $ret; }
// print_r($_SESSION); $accessToken = $soundcloud->accessToken($_GET['code']); // $_SESSION['token'] = $accessToken['access_token']; // } // else // { // print_r($_SESSION); // $soundcloud->setAccessToken($_SESSION['token']); // print_r($accessToken); // } echo '<pre>'; } catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) { exit($e->getMessage()); } try { $me = json_decode($soundcloud->get('me'), true); print_r($me); $tracks = json_decode($soundcloud->get('tracks', array('user_id' => $me['id'])), true); print_r($tracks); $track_url = 'https://soundcloud.com/ankit-bhatt-9/test-4'; $embed_info = json_decode($soundcloud->get('oembed', array('url' => $track_url))); print_r($embed_info->html); } catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) { exit($e->getMessage()); } try { $url = 'I:\\XAMPP\\htdocs\\sound\\synup.mp3'; $mytrack = array('track[title]' => 'sync', 'track[asset_data]' => '@' . $url); $soundcloud->setAccessToken($access_token); $track = json_decode($soundcloud->post('/tracks', $mytrack)); echo '<p><b>Congrats your file successfully uploaded to <a target="_blank" href="' . $track->permalink_url . '">' . $track->permalink_url . '</a>';
function mg_get_soundcloud_embed($track_url) { include_once MG_DIR . '/classes/soundcloud-api/Services/Soundcloud.php'; $pub = '69c06a70f88e8ec80a414ae55dab369c'; $sec = 'f7fe291ae99ef71d3fd81c084897309e'; $callback = 'http://www.lcweb.it/'; $client = new Services_Soundcloud($pub, $sec, $callback); $client->setCurlOptions(array(CURLOPT_FOLLOWLOCATION => 1)); $track = json_decode($client->get('resolve', array('url' => $track_url))); $autoplay = get_option('mg_audio_autoplay') ? 'true' : 'false'; return '<iframe width="100%" height="166" scrolling="no" frameborder="no" src="//w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/' . $track->id . '&color=ff5500&auto_play=' . $autoplay . '&hide_related=true&show_artwork=true" style="margin-top: 7px;"></iframe>'; }
* returns array which includes access token*/ $accessToken = $scUser->accessToken($_GET['code']); //set SESSION to access token(from accesstoken array) $_SESSION['token'] = $accessToken['access_token']; } else { /*set our classes accesstoken property to the session data(set after following initial connect to soundcloud link) this will happen on refresh of page */ $scUser->setAccessToken($_SESSION['token']); } } catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) { exit($e->getMessage()); } //run search $username = '******'; $tracksSearchQ = json_decode($scUser->get('tracks', array('q' => $username)), true); //print_r($tracksSearchQ); //print_r($tracksSearchQ[0]['permalink_url']); //select trackurls(permalink url) from returned array //cycle through top level array foreach ($tracksSearchQ as $trackInfo) { //cycle through track info. array foreach ($trackInfo as $key => $value) { // echo "$key . 'is' . $value . '<br>'"; //and identify urls if ($key == 'permalink_url') { //not sure what this does $scUser->setCurlOptions(array(CURLOPT_FOLLOWLOCATION => 1)); //echo "$key . 'is' . $value . '<br>'"; try { //set var to array urls
function sc_render_manager_page() { require_once plugin_dir_path(__FILE__) . 'includes/Services/Soundcloud.php'; require_once plugin_dir_path(__FILE__) . 'includes/class/pagination.class.php'; $options = get_option('sc_options'); $client = new Services_Soundcloud($options['client_id'], $options['client_secret'], $options['callback_url']); // Error message if app settings are not filled in. if ($options['client_id'] == "Your client ID goes here" || $options['client_secret'] == "Your client secret goes here" || $options['callback_url'] == "Your callback url goes here") { echo '<div class="error fade"><p>Warning: One or more of the required fields have not been set. Please insure that your cliend ID, client secret, and callback url are defined <a href="' . admin_url('options-general.php?page=soundcloud/soundcloud.php') . '">HERE</a>.</br>Soundcloud functionality will not be available until these fields are set.</p></div></br>'; } // SoundCloud login/logout if (isset($_GET['code']) || get_option('soundcloud_access_token')) { echo '<a href="' . admin_url('admin.php?page=soundcloud-disconnect') . '" class="soundcloud-icon"><img src="' . plugins_url() . '/soundcloud/includes/images/btn-disconnect-l.png" alt="Disconnect"/></a>'; try { $client = new Services_Soundcloud($options['client_id'], $options['client_secret'], $options['callback_url']); if (isset($_GET['code'])) { $accessToken = $client->accessToken($_GET['code']); update_option('soundcloud_access_token', $accessToken['access_token']); update_option('soundcloud_refresh_token', $accessToken['refresh_token']); update_option('soundcloud_token_expiration', time() + $accessToken['expires_in']); } else { if (get_option('soundcloud_access_token')) { // refresh token code deprecated due to non-expiring tokens /*if (time() > get_option('soundcloud_token_expiration')) { $client->setAccessToken(get_option('soundcloud_access_token')); // refresh access token $accessToken = $client->accessTokenRefresh(get_option('soundcloud_refresh_token')); //$client->setAccessToken($accessToken['access_token']); update_option('soundcloud_access_token', $accessToken['access_token']); update_option('soundcloud_refresh_token', $accessToken['refresh_token']); update_option('soundcloud_token_expiration', time() + $accessToken['expires_in']); } else {*/ $client->setAccessToken(get_option('soundcloud_access_token')); //} } } } catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) { var_dump($e->getMessage()); exit; } } else { echo '<a href="' . $client->getAuthorizeUrl(array('scope' => 'non-expiring')) . '" class="soundcloud-icon"><img src="' . plugins_url() . '/soundcloud/includes/images/btn-connect-sc-l.png" alt="Connect with Soundcloud"/></a>'; } // tab switching if (isset($_GET['tab'])) { sc_admin_tabs($_GET['tab']); $tab = $_GET['tab']; } else { sc_admin_tabs('user'); $tab = 'user'; } if (get_option('soundcloud_access_token')) { try { $me = json_decode($client->get('me'), true); } catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) { var_dump($e->getMessage()); exit; } echo '<h3>Logged in as: ' . $me['username'] . '</h3>'; } else { echo '<h3>Not logged in.</h3>'; } switch ($tab) { case 'user': try { $me = json_decode($client->get('me'), true); ?> <form action="" method="post"> <table class="form-table"> <tr valign="top"> <th scope="row"><label for="username">Username:</label></th> <td><input type="text" name="username" value="<?php echo $me['username']; ?> " size="30" class="regular-text code"></td> </tr> <tr valign="top"> <th scope="row"><label for="permalink">Permalink:</label></th> <td><input type="text" name="permalink" value="<?php echo $me['permalink']; ?> " size="30" class="regular-text code"></td> </tr> <tr valign="top"> <th scope="row"><label for="description">Description:</label></th> <td><input type="text" name="description" value="<?php echo $me['description']; ?> " size="30" class="regular-text code"></td> </tr> <tr valign="top"> <th scope="row"><label for="website">Website:</label></th> <td><input type="text" name="website" value="<?php echo $me['website']; ?> " size="30" class="regular-text code"></td> </tr> <tr valign="top"> <th scope="row"><label for="website_title">Website Name:</label></th> <td><input type="text" name="website_title" value="<?php echo $me['website_title']; ?> " size="30" class="regular-text code"></td> </tr> </table> <p class="submit"><input type="submit" value="Update Account" class="button-primary"></p> </form> <?php if (isset($_POST['username']) || isset($_POST['permalink']) || isset($_POST['description']) || isset($_POST['website']) || isset($_POST['website_title'])) { $response = json_decode($client->put('me', array('user[username]' => $_POST['username'], 'user[permalink]' => $_POST['permalink'], 'user[description]' => $_POST['description'], 'user[website]' => strlen($_POST['website']) ? $_POST['website'] : null, 'user[website_title]' => strlen($_POST['website_title']) ? $_POST['website_title'] : null))); } } catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) { var_dump($e->getMessage()); exit; } break; case 'upload': ?> <form action="" enctype="multipart/form-data" method="post" id ="add_track"> <table class="form-table"> <tr valign="top"> <th scope="row"><label for="track_title">Track title:</label></th> <td><input type="text" name="track_title" size="30" class="regular-text code"></td> </tr> <tr valign="top"> <th scope="row"><label for="track_file">Please specify a track:</label></th> <td><input type="file" name="track_file" id="track_file" size="40" accept="audio/*" class="regular-text code"></td> </tr> <tr valign="top"> <th scope="row"><label for="track_art">Please specify track artwork:</label></th> <td><input type="file" name="track_art" id="track_art" size="40" accept="image/*" class="regular-text code"></td> </tr> <tr valign="top"> <th scope="row"><label for="tag">Track tags:</label></th> <td><input type="text" name="tag" id="tag" size="30"><input type="button" id="add_tag" name="add_tag" class="button" value="Add Tag" /> <br><ul id="tags"></ul></td> </tr> <tr valign="top"> <th scope="row"><label for="sharing">Track privacy:</label></th> <td><select name="sharing"> <option value="public">Public</option> <option value="private">Private</option> </select></td> </tr> </table> <p class="submit"><input type="submit" value="Upload" class="button-primary"></p> </form> <?php try { $tmp_file = '/tmp/' . stripslashes($_FILES['track_file']['name']); $tmp_art_file = '/tmp/' . stripslashes($_FILES['track_art']['name']); if (move_uploaded_file($_FILES['track_file']['tmp_name'], $tmp_file) && move_uploaded_file($_FILES['track_art']['tmp_name'], $tmp_art_file)) { // upload audio file $track = json_decode($client->post('tracks', array('track[title]' => $_POST['track_title'], 'track[asset_data]' => '@' . $tmp_file, 'track[artwork_data]' => '@' . $tmp_art_file, 'track[tags]' => strlen($_POST['tags']) ? $_POST['tags'] : null, 'track[sharing]' => $_POST['sharing']))); unlink(realpath($tmp_file)); unlink(realpath($tmp_art_file)); } } catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) { var_dump($e->getMessage()); exit; } break; case 'tracks': try { echo '</br>'; $page_size = 5; // Pagination code $p = new pagination(); $p->items($me['track_count']); $p->limit($page_size); // Limit entries per page $p->target("admin.php?page=soundcloud-manager&tab=tracks"); $p->currentPage($_GET[$p->paging]); // Gets and validates the current page $p->calculate(); // Calculates what to show $p->parameterName('paging'); $p->adjacents(1); //No. of page away from the current page if (!isset($_GET['paging'])) { $p->page = 1; } else { $p->page = $_GET['paging']; } if ($p->page == 1) { // get first page of tracks $tracks = json_decode($client->get('users/' . $me['id'] . '/tracks', array('order' => 'created_at', 'limit' => $page_size))); } else { // get additional pages of tracks $tracks = json_decode($client->get('users/' . $me['id'] . '/tracks', array('order' => 'created_at', 'limit' => $page_size, 'offset' => $page_size * $page))); } ?> <div class="tablenav"> <div class='tablenav-pages'> <?php echo $p->show(); ?> </div> </div> <?php foreach ($tracks as $track) { $client->setCurlOptions(array(CURLOPT_FOLLOWLOCATION => 1)); $embed_info = json_decode($client->get('oembed', array('url' => $track->permalink_url))); // render the html for the player widget echo $embed_info->html . '</br></br>'; } } catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) { var_dump($e->getMessage()); exit; } break; } }
function get_soundcloud_widget_code($track, $auto_play = false) { //get soundcloud options require_once 'includes/Services/Soundcloud.php'; $sc_options = get_option('soundcloud_settings'); if ($sc_options) { $sc_id = $sc_options['sc_client_id']; $sc_secret = $sc_options['sc_client_secret']; $sc_token = $sc_options['sc_client_access_token']; $sc_redirect_uri = $sc_options['sc_redirect_uri']; } $soundcloud = new Services_Soundcloud($sc_id, $sc_secret, $sc_redirect_uri); if ($sc_token && $sc_id && $sc_secret) { $soundcloud->setAccessToken($sc_token); } else { //display an error stating that the user needs to authenticate first return '<div style="color:red;"><p><strong>SoundCloud Ultimate Error: You need to authenticate your connection to the SoundCloud API. <br />Please go to the SoundCloud Ultimate plugin settings and configure your API credentials.</strong></p></div>'; } //first check if track has finished uploading //$tracks = $soundcloud->get('tracks/'); $soundcloud->setCurlOptions(array(CURLOPT_FOLLOWLOCATION => 1)); try { $track_list = $soundcloud->get('me/tracks'); } catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) { if ($e->getHttpCode() == '302') { //Special case - 302 received - continue to try and render the widget..... $track_widget = $this->track_widget($soundcloud, $track, $auto_play); return $track_widget; } else { return '<div style="color:red;"><p><strong>SoundCloud Ultimate Error: Could not display your SoundCloud track - Error code (' . $e->getHttpCode() . ').</strong></p></div>'; } } $tracks = json_decode($track_list, true); foreach ($tracks as $item) { if ($item['permalink_url'] == $track && $item['state'] != 'finished') { return false; } } try { $track_embed_data = $soundcloud->get('oembed', array('url' => $track, 'auto_play' => $auto_play)); } catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) { if ($e->getHttpCode() == '302') { //Special case - 302 received - continue to try and render the widget..... $track_widget = $this->track_widget($soundcloud, $track, $auto_play); return $track_widget; } else { if ($e->getHttpCode() == '404') { return '<div style="color:red;"><p><strong>SoundCloud Ultimate Error: The track you specified in the shortcode does not exist in your account.</strong></div>'; } else { return '<div style="color:red;"><p><strong>SoundCloud Ultimate Error: Could not display your SoundCloud track - Error code (' . $e->getHttpCode() . ').</strong></p></div>'; } } } //$embed_info = json_decode($soundcloud->get('oembed', array('url' => $track))); $embed_info = json_decode($track_embed_data); //now get the html code for the player widget //print $embed_info->html; $track_widget = $embed_info->html; return $track_widget; }
/** * Import the podcasts using the SoundCloud API * * @since 1.0.0 */ public function import_podcasts() { require_once plugin_dir_path(__FILE__) . 'assets/php-soundcloud-master/Services/Soundcloud.php'; $output = ''; $options = get_option('hbi_soundcloud_podcasts_settings'); $client = new Services_Soundcloud($options['soundcloud_client_id'], $options['soundcloud_client_secret'], $options['soundcloud_redirect_uri']); $soundcloud_user_id = $options['soundcloud_user_id']; $access_token = $client->setAccessToken(); $current_time = current_time('timestamp') - 21600; // grab the tracks $tracks = json_decode($client->get('users/' . $soundcloud_user_id . '/tracks', array('created_at[from]' => date('Y-m-d', $current_time)))); if (count($tracks)) { $output .= "Currently processing " . count($tracks) . " tracks.\r\n"; $totalimport = 0; // loop through the tracks foreach ($tracks as $track) { if ($track->downloadable != 1) { $output .= "Track " . $track->id . " is not set to downloadable. \r\n"; $downloadable = <<<EOH <track> <downloadable>true</downloadable> </track> EOH; try { $client->put('tracks/' . $track->id, $downloadable, array(CURLOPT_HTTPHEADER => array('Content-Type: application/xml'))); $output .= "Updated Track " . $track->id . " in SoundCloud to make sure it was downloadable.\r\n\r\n"; } catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) { $output .= "Error setting track " . $track->id . "to be downloadable. " . $e->getMessage() . "\r\n\r\n"; } } $args = array('post_type' => 'podcasts', 'meta_key' => 'soundcloud-track-id', 'post_status' => 'any', 'meta_value' => $track->id); $test = get_posts($args); if (count($test) == 0) { $downloadURL = 'http://feeds.soundcloud.com/stream/' . $track->id . '-101sports-' . $track->permalink . '.mp3'; $show = 'zach-and-rammer'; // Added in trim to try to fix all of the wrong things happening from extra spaces $release = str_replace(' ', '-', strtolower(trim($track->release))); $release = str_replace('&', 'and', $release); if ($release != '') { $show = $release; } $content = array('post_type' => 'podcasts', 'post_status' => 'publish', 'post_title' => $track->title, 'post_content' => $track->description, 'tax_input' => array('podcast-segment' => array(trim(strtolower($track->label_name))))); $post_id = wp_insert_post($content); $term = term_exists($track->label_name, 'podcast_segment'); wp_set_post_terms($post_id, $term['term_id'], 'podcast_segment'); add_post_meta($post_id, 'soundcloud-file', $downloadURL); add_post_meta($post_id, 'use-soundcloud', 1); add_post_meta($post_id, 'soundcloud-shortcode', '[soundcloud url="http://api.soundcloud.com/tracks/' . $track->id . '" params="" width=" 100%" height="166" iframe="true" /]'); add_post_meta($post_id, 'soundcloud-track-id', $track->id); add_post_meta($post_id, 'podcast-show', $show); $totalimport++; $output .= "Imported " . $track->title . "(" . $track->id . ") into the system.\r\n"; } } $output .= "Finished processing " . count($tracks) . " tracks. " . $totalimport . " have been imported into the system.\r\n"; } }