承接 janyksteenbeek/soundcloud 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

janyksteenbeek/soundcloud

Composer 安装命令:

composer require janyksteenbeek/soundcloud

包简介

PHP SDK for Soundcloud API

README 文档

README

A PHP SDK for the Soundcloud API, built on Saloon.

Installation

composer require janyksteenbeek/soundcloud

Basic Usage

use Janyk\Soundcloud\Soundcloud;
use Saloon\Http\Auth\TokenAuthenticator;
use Saloon\Http\Auth\QueryAuthenticator;

// Create a new Soundcloud client
$client = new Soundcloud();

// Authenticate with OAuth token (for API endpoints requiring user authentication)
$client->authenticate(new TokenAuthenticator('YOUR_ACCESS_TOKEN'));

// Or authenticate with client ID for endpoints that only require API key
// $client->authenticate(new QueryAuthenticator('client_id', 'YOUR_CLIENT_ID'));

// Get information about the authenticated user (requires OAuth authentication)
$me = $client->me()->getMyProfile();

// Get a track by ID (can work with just client ID authentication)
$track = $client->tracks()->getTrack(123456789, null);

// Search for tracks
$searchResults = $client->search()->tracks('query', null, null, 10, null, true);

// Get user likes (requires OAuth authentication)
$likes = $client->likes()->getMyLikes(10, true);

// Get a user's public profile (can work with just client ID)
$user = $client->users()->getUser(123456789);

Internal Mode

Soundcloud has an alternative API endpoint that allows more functionality with just a client ID. The SDK provides a simple way to enable this "internal" mode:

use Janyk\Soundcloud\Soundcloud;

$client = new Soundcloud();

// Enable internal mode with your client ID
// This automatically sets the base URL to api-v2.soundcloud.com and authenticates with your client ID
$client->enableInternalMode('YOUR_CLIENT_ID');

// Now you can access endpoints that would normally require OAuth with just your client ID
$track = $client->tracks()->getTrack(123456789, null);

// Check if internal mode is enabled
if ($client->isInternalModeEnabled()) {
    // do something specific to internal mode
}

// You can disable internal mode if needed
$client->disableInternalMode();

When internal mode is enabled:

  • The base URL changes to api-v2.soundcloud.com
  • The client automatically uses Query Authentication with your client ID
  • Some endpoints that normally require OAuth authentication become accessible with just the client ID

This is useful for building applications that need to access certain Soundcloud features without requiring user authentication.

Authentication

OAuth Authentication

The Soundcloud API uses OAuth 2.0 for authentication. This SDK provides methods to handle the OAuth flow.

Authorization Flow

use Janyk\Soundcloud\Soundcloud;
use Saloon\Http\Auth\TokenAuthenticator;

// Step 1: Create a client for authorization
$soundcloud = new Soundcloud();

// Step 2: Get the authorization URL to redirect the user
$clientId = 'YOUR_CLIENT_ID';
$redirectUri = 'YOUR_REDIRECT_URI';
$responseType = 'code';
$state = bin2hex(random_bytes(16)); // Generate a random state for CSRF protection

// Store the state in the session for validation later
$_SESSION['oauth_state'] = $state;

// Get the authorization URL
$response = $soundcloud->oauth()->deprecatedUseSecureConnect(
    $clientId, 
    $redirectUri, 
    $responseType, 
    $state
);

// Redirect the user to the authorization URL
// This is normally done with a proper redirect in your framework
$authUrl = $response->body();
// header('Location: ' . $authUrl);

Token Exchange and Using the API with Authentication

use Janyk\Soundcloud\Soundcloud;
use Saloon\Http\Auth\TokenAuthenticator;

// Step 3: After the user is redirected back to your app
// Exchange the authorization code for an access token
// This would typically be done in a separate callback route

// Verify state parameter to prevent CSRF attacks
if ($_GET['state'] !== $_SESSION['oauth_state']) {
    die('Invalid state parameter');
}

// The code will be in the query parameters after redirect
$code = $_GET['code'];

// Exchange code for token (this would be implemented by you using the token endpoint)
// $tokenResponse = $yourHttpClient->post('https://api.soundcloud.com/oauth2/token', [
//     'client_id' => 'YOUR_CLIENT_ID',
//     'client_secret' => 'YOUR_CLIENT_SECRET',
//     'grant_type' => 'authorization_code',
//     'code' => $code,
//     'redirect_uri' => 'YOUR_REDIRECT_URI'
// ]);
// $accessToken = $tokenResponse->json()['access_token'];

// For demonstration purposes only:
$accessToken = 'YOUR_ACCESS_TOKEN';

// Step 4: Create a new authenticated client
$soundcloud = new Soundcloud();
$soundcloud->authenticate(new TokenAuthenticator($accessToken));

// Now you can make authenticated requests
$myProfile = $soundcloud->me()->getMyProfile();
$myPlaylists = $soundcloud->me()->getMyPlaylists(10, true);

Simple API Key Authentication

For some endpoints, you can use just the client ID as an API key:

use Janyk\Soundcloud\Soundcloud;
use Saloon\Http\Auth\QueryAuthenticator;

// Create client with API key auth
$soundcloud = new Soundcloud();
$soundcloud->authenticate(new QueryAuthenticator('client_id', 'YOUR_CLIENT_ID'));

// Now you can make requests that only require client_id
$track = $soundcloud->tracks()->getTrack(123456789, null);

Available Resources

The SDK provides access to the following Soundcloud API resources:

Tracks

Access and manage tracks on Soundcloud:

// Get a track
$track = $client->tracks()->getTrack($trackId, $secretToken);

// Upload a track
$response = $client->tracks()->uploadTrack();

// Update a track
$response = $client->tracks()->updateTrack($trackId);

// Delete a track
$response = $client->tracks()->deleteTrack($trackId);

// Get track comments
$comments = $client->tracks()->getTrackComments($trackId, $limit, $offset, $linkedPartitioning);

// Create a comment on a track
$response = $client->tracks()->createComment($trackId);

// Get track favoriters
$favoriters = $client->tracks()->trackFavoriters($trackId, $limit, $linkedPartitioning);

// Get track reposters
$reposters = $client->tracks()->trackReposters($trackId, $limit);

// Get track streams
$streams = $client->tracks()->trackStreams($trackId, $secretToken);

// Get related tracks
$related = $client->tracks()->relatedTracks($trackId, $access, $limit, $offset, $linkedPartitioning);

Users

Access and manage user information:

// Get a user by ID
$user = $client->users()->getUser($userId);

// Get user playlists
$playlists = $client->users()->getUserPlaylists($userId, $limit, $linkedPartitioning);

// Get user tracks
$tracks = $client->users()->getUserTracks($userId, $limit, $linkedPartitioning);

// Get user followers
$followers = $client->users()->getUserFollowers($userId, $limit, $linkedPartitioning);

// Get users followed by a user
$followings = $client->users()->getUserFollowings($userId, $limit, $linkedPartitioning);

Playlists

Access and manage playlists:

// Get a playlist
$playlist = $client->playlists()->getPlaylist($playlistId, $secretToken);

// Get playlist tracks
$tracks = $client->playlists()->getPlaylistTracks($playlistId, $secretToken);

Me (Current User)

Access and manage the authenticated user's information:

// Get current user's profile
$profile = $client->me()->getMyProfile();

// Get current user's playlists
$playlists = $client->me()->getMyPlaylists($limit, $linkedPartitioning);

// Get current user's tracks
$tracks = $client->me()->getMyTracks($limit, $linkedPartitioning);

// Get current user's followers
$followers = $client->me()->getMyFollowers($limit, $linkedPartitioning);

// Get users followed by current user
$followings = $client->me()->getMyFollowings($limit, $linkedPartitioning);

Search

Search for resources on Soundcloud:

// Search for tracks
$tracks = $client->search()->tracks($query, $tags, $genres, $limit, $offset, $linkedPartitioning);

// Search for playlists
$playlists = $client->search()->playlists($query, $limit, $offset, $linkedPartitioning);

// Search for users
$users = $client->search()->users($query, $limit, $offset, $linkedPartitioning);

Likes

Access and manage likes:

// Get user likes
$likes = $client->likes()->getUserLikes($userId, $limit, $linkedPartitioning);

// Get current user's likes
$likes = $client->likes()->getMyLikes($limit, $linkedPartitioning);

Reposts

Access and manage reposts:

// Get user reposts
$reposts = $client->reposts()->getUserReposts($userId, $limit, $linkedPartitioning);

// Get current user's reposts
$reposts = $client->reposts()->getMyReposts($limit, $linkedPartitioning);

OAuth

Handle OAuth authentication:

// Get OAuth token
$token = $client->oauth()->getToken();

License

This package is open-sourced software licensed under the MIT license.

janyksteenbeek/soundcloud 适用场景与选型建议

janyksteenbeek/soundcloud 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 04 月 06 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 janyksteenbeek/soundcloud 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 janyksteenbeek/soundcloud 我们能提供哪些服务?
定制开发 / 二次开发

基于 janyksteenbeek/soundcloud 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 8
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 14
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-04-06