我正在寫一個WordPress插件來從Bandcamp獲取所有個人發佈(唱片)並顯示在WordPress網站上。fetch bandcamp用戶discography json in wordpress
參考:http://bandcamp.com/developer
我設法拿到第呼籲唱片,並將其存儲在數據庫中,並使用從工地附近的JSON文件中的任何信息。
/*
* assign global variables
*
*/
$plugin_url = WP_PLUGIN_URL . '/bandcamp-integration';
$options = array();
/*
* create admin menu link under 'Settings > Bandcamp'
*
*/
function bodhi_bi_admin_menu() {
add_options_page(
'Bandcamp Integration',
'Bandcamp',
'manage_options',
'bi-options',
'bodhi_bi_options_page'
);
}
add_action('admin_menu', 'bodhi_bi_admin_menu');
/*
* create admin options page under 'Settings > Bandcamp'
*
*/
function bodhi_bi_options_page() {
if(! current_user_can('manage_options')) {
wp_die('Nice try, you cannot access this page.');
}
global $plugin_url;
global $options;
if(isset($_POST['bodhi_bi_band_id_form_submitted'])) {
$hidden_field = esc_html($_POST['bodhi_bi_band_id_form_submitted']);
if($hidden_field == 'Y') {
$bodhi_bi_band_id = esc_html($_POST['bodhi_bi_band_id']);
$bodhi_bi_bc_discog = bodhi_bi_get_bandcamp_discog($bodhi_bi_band_id);
$options['bodhi_bi_band_id'] = $bodhi_bi_band_id;
$options['bodhi_bi_bc_discog'] = $bodhi_bi_bc_discog;
$options['last_updated'] = time();
update_option('bodhi_bi_discog', $options);
}
}
$options = get_option('bodhi_bi_discog');
if($options != '') {
$bodhi_bi_band_id = $options['bodhi_bi_band_id'];
$bodhi_bi_bc_discog = $options['bodhi_bi_bc_discog'];
}
// options page markup
require('includes/options-page-wrapper.php');
}
/*
* get bandcamp discography
*
*/
function bodhi_bi_get_bandcamp_discog($bodhi_bi_band_id) {
// WHY YOU NOT WORK???
//$json_feed_discog_url = 'http://api.bandcamp.com/api/band/3/discography?key=' . $bc_dev_key . '&band_id=' . $bodhi_bi_band_id;
// WORKS
$json_feed_discog_url = 'http://api.bandcamp.com/api/band/3/discography?key=ACTUALBANDCAMPKEYHERE&band_id=' . $bodhi_bi_band_id;
$args = array('timeout' => 120);
$json_feed_discog = wp_remote_get($json_feed_discog_url, $args);
$bodhi_bi_bc_discog = json_decode($json_feed_discog['body']);
return $bodhi_bi_bc_discog;
}
此存儲唱片JSON飼料中的數據庫,所以我可以像這樣從它調用的信息:
// count total number of releases in discography
$total_releases = count($bodhi_bi_bc_discog->discography);
echo 'Number of releases: ' . $total_releases;
OR
// list layout (inline styles just for testing)
foreach($bodhi_bi_bc_discog->discography as $result) {
echo '<li style="width:100%;list-style:none;border-bottom:1px solid #ccc;padding-bottom:2.2em;overflow:hidden;">';
echo '<h2 style="font-weight:normal;">' . $result->title . '</h2>';
echo '<img style="float:left;margin-right:1em;" src="' . $result->large_art_url . '" alt="' . $result->title . '" />';
echo $result->artist;
echo '<br />';
echo $result->title;
echo '<br />';
echo '<a href="' .$result->url . '?action=buy">BUY</a>';
echo '<br />';
echo '<a href="' .$result->url . '?action=download">DOWNLOAD</a>';
echo '</li>';
}
我的問題是,Bandcamp讓你拉各專輯的信息與唱片分開,然後再分開每個曲目的信息(爲了獲得流媒體網址和每個曲目的特定信息)。
我設法將album_id和track_id的所有內容都放到數組中,然後foreach - 將它們添加到正確的url以檢索json,但這只是在頁面模板中,我希望它全部在功能。
$album_ids = array();
$track_ids = array();
for($i=0; $i<=count($bodhi_bi_bc_discog->discography); $i++) {
if ($bodhi_bi_bc_discog->{'discography'}[$i]->{'album_id'} != '') {
$album_ids[$i] = $bodhi_bi_bc_discog->{'discography'}[$i]->{'album_id'};
}
if ($bodhi_bi_bc_discog->{'discography'}[$i]->{'track_id'} != '') {
$track_ids[$i] = $bodhi_bi_bc_discog->{'discography'}[$i]->{'track_id'};
}
// echo '<br />loop works';
}
foreach($album_ids as $album_id) {
$feed_url = 'http://api.bandcamp.com/api/album/2/info?key=' . $bc_dev_key . '&album_id=' . $album_id;
echo '<br />' . $feed_url;
}
foreach($track_ids as $track_id) {
$feed_url = 'http://api.bandcamp.com/api/track/3/info?key=' . $bc_dev_key . '&track_id=' . $track_id;
echo '<br />' . $feed_url;
}
所以,我真的很感謝一些幫助獲得所有這些飼料和存儲在數據庫中,就像我爲主要的唱片一樣。 我也需要做類似的事情來獲得每張專輯的曲目。最終,我只需要訪問每一點信息,從數據庫中完整的唱片,而不必調用bandcamp api這麼多該死的時間,尤其是在頁面加載時。
我希望這是有道理的!感謝您提前提供任何幫助!