Techumber
Home Blog Work

SoundCloud Tutorials:Uploading Audio Files

Published on November 25, 2012

This is SoundCloud Tutorial series in previous post we have learned how to Authenticateuser with using SoundCloud PHP SDK. In today’s tutorial we will learn how to upload Audio to SounldCloud using the SoundCloud PHP API. SoundCloud Tutorials:Uploading Audio Files

Demo

Download

1)Sound Cloud Tutorials:Getting Started 2)Sound Cloud Tutorials:Authentication 3a)Sound Cloud Tutorials:Uploading Audio Files 3b)Sound Cloud Tutorials:Uploading Audio Files(AJAX) 4)Sound Cloud Tutorials:Playing Audio Files As i suggested in previous post don’t use default SoundCloud SDK. It is full of bugs. I made the bug free version by rectifying errors. So download the code from above link in that lib folder is the SoundCloud PHP SDK. File Structure Since it is the continues part made the folder structure just as we made it in previous lesson. SoundCloud Tutorials:Authentication Folder StructureAlong with the above structure we need to another file uploadsound.php. Another change we need to do is on callback.php. See the below code. callback.php

<?php
include("config.php");
try {
$accessToken = $soundcloud->accessToken($_GET['code']);
} catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
exit($e->getMessage());
}
try {
$me = json_decode($soundcloud->get('me'), true);
} catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
exit($e->getMessage());
}

$user_data = array(
'access_token' => $accessToken['access_token'],
'id' => $me['id'],
'username' => $me['username'],
'name' => $me['full_name'],
'avatar' => $me['avatar_url']
);

header("Location:uploadsound.php?access_token=".$user_data['access_token']);

In this only change is redirecting to uploadsound.php along with the access_token value. As i said in previous post we will use the access_token here. Once you have authenticated you will get this access_token. This access_token will be used for all our tasks until we signed out or close the browser. uploadsound.php(HTML)

<html>
  <body>
    <form action="" method="post" enctype="multipart/form-data">
      <input
        type="hidden"
        name="access_token"
        value="<?php echo $_GET['access_token']; ?>"
      />
      <br />
      Audio Name:<input
        type="text"
        name="audioname"
        placeholder="My audio"
      /><br /><br />
      Audio File: <input type="file" name="audiofile" id="audiofile" />
      <br /><br />
      <input type="submit" />
    </form>
  </body>
</html>

This is simple html we will use it for upload audio file here we storing access_token as hidden field. uploadsound.php(PHP Script)

<?php
include("config.php");
$soundcloud->setAccessToken($_POST['access_token']);

$mytrack = array(
'track[title]' => $_POST["audioname"],
'track[asset_data]' => '@'.$_FILES["audiofile"]["tmp_name"]
);

$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>';
?>

When you selected the audio file and submit it, This script will start executing. In this first we are setting access*taken. Preparing *$mytrack_ array and posting it to SoundCloud. The output will be the audios path on SoundCloud. uploadsound.php(CSS)

#upload_result {
  border: 1px solid #cccccc;
  border-radius: 4px;
  padding: 0 20px 20px;
}
input[type="text"],
input[type="file"] {
  border: 1px solid #cccccc;
  box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
  outline: none;
  padding: 4px 6px;
  font-size: 14px;
  line-height: 20px;
  color: #555555;
  border-radius: 3px;
  width: 325px;
  float: right;
}
input[type="submit"] {
  display: block;
  color: #fff;
  font-weight: bold;
  cursor: pointer;
  margin: 5px 0 0 60px;
  padding: 6px 10px;
  border: 1px solid #777;
  background: #333537;
  border-radius: 5px;
}

I never like default styles of html so here I am putting some styles. Compleate uploadsound.php

<?php
if($_POST)
{
include("config.php");
$soundcloud->setAccessToken($_POST['access_token']);

$mytrack = array(
'track[title]' => $_POST["audioname"],
'track[asset_data]' => '@'.$_FILES["audiofile"]["tmp_name"]
);

$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>';
}
else
if($_GET['access_token']){
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Sound Cloud Tutorials:Uploading Audio Files:demos.techumber.com</title>
<style type="text/css">
body {
font-family: "Comic Sans MS", Helvetica, Arial, sans-serif;
font-size: 14px;
line-height: 32px;
color: #333333;
font-weight: normal;
}
.container{
width: 500px;
margin: 0 auto;
}
#upload_result{
border: 1px solid #cccccc;
border-radius: 4px;
padding: 0 20px 20px;
}
input[type="text"],input[type="file"] {
border: 1px solid #cccccc;
box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
outline: none;
padding: 4px 6px;
font-size: 14px;
line-height: 20px;
color: #555555;
border-radius: 3px;
width: 325px;
float: right;
}
input[type="submit"] {
display: block;
color: #fff;
font-weight: bold;
cursor: pointer;
margin: 5px 0 0 60px;
padding: 6px 10px;
border: 1px solid #777;
background: #333537;
border-radius: 5px;
}
</style>
</head>
<body>
<div class="container">
<div id="upload_result">
<p>Download this <a href="airtelbirdmix.mp3">airtelbirdmix.mp3</a>(80k) file and use it for upload if you want to test fast</p>
<form action="" method="post" enctype="multipart/form-data">
<input type="hidden" name="access_token" value="<?php echo $_GET['access_token']; ?>" />
<br />
Audio Name:<input type="text" name="audioname" placeholder="My audio" /><br /><br />
Audio File: <input type="file" name="audiofile" id="audiofile" />
<br /><br />
<input type="submit" />
</form>
</div>
</div>
</body>
</html>

<?php
}
else{
header("Location://techumber.com");
}
?>

That’s it Hope your like it. When I am developing this script I felt like missing some thing(AJX). Yap in the next post i will write about how to upload audio in ajax.