noweh/laravel-soundcloud

Laravel 的 Soundcloud API 封装器。

1.2.1 2023-09-20 14:14 UTC

This package is auto-updated.

Last update: 2024-09-20 16:51:40 UTC


README

SoundCloud Laravel PHP MIT Licensed last version Downloads

A Laravel Wrapper for the SoundCloud REST API endpoints.

安装

首先,您需要在 composer.json 中添加此组件。

composer require noweh/laravel-soundcloud

使用 composer update 更新您的包,或使用 composer install 安装。

Laravel 使用包自动发现,因此不需要您手动添加 ServiceProvider。

Laravel 没有自动发现

Noweh\SoundcloudApi\SoundcloudServiceProvider::class,

要使用外观,请在 app.php 中添加以下内容

'Soundcloud' => Noweh\SoundcloudApi\SoundcloudFacade::class,

服务提供者

更新 composer 后,将 ServiceProvider 添加到 config/app.php 中的 providers 数组中

配置文件

接下来,您必须迁移配置

php artisan vendor:publish --provider="Noweh\SoundcloudApi\SoundcloudServiceProvider"

⚠️ {CALLBACK_URL} 必须与您的 SoundCloud 账户中指示的相同。

用法

⚠️ 从 2021 年 7 月 开始,大多数对 SoundCloud REST API 的调用都需要一个 access_token

您必须将用户重定向到 SoundCloud 登录页面

return redirect(\Soundcloud::getAuthorizeUrl('a_custom_param_to_retrieve_in_callback'));

在您的回调 URL 上,您可以调用 GET/POST/PUT/DELETE 方法。当 URL 中存在 code 参数时,将自动生成 access_token

如果要在另一个页面上使用 API 调用,您必须手动设置这些数据

\Soundcloud::setCode('3-134981-158678512-IwAXqypKWlDJCF');

// API Call
...

获取播放器嵌入

此调用不需要 access_token。

要检索指向用户、设置或播放列表的任何 SoundCloud URL 的小部件嵌入代码,请执行以下操作

// Required parameter
$url = 'https://soundcloud.com/......';

// Optional parameters
$maxheight = 180;
$sharing = true;
$liking = true;
$download = false;
$show_comments = true;
$show_playcount = false;
$show_user = false;

try {
    $response = \Soundcloud::getPlayerEmbed($url, $maxheight, $sharing, $liking, $download, $show_comments, $show_playcount, $show_user)
} catch (Exception $e) {
    exit($e->getMessage());
}

GET

try {
    $response = \Soundcloud::get('users/{CLIENT_ID}/tracks');
} catch (Exit $e) {
    exit($e->getMessage());
}

分页 GET

我们 API 的多数结果以集合的形式返回。默认情况下,返回的集合中项目数量限制为 50,最大值为 200。大多数端点支持 linked_partitioning 参数,允许您浏览集合。当传递此参数时,如果存在更多结果,则响应将包含 next_href 属性。要获取下一页的结果,只需遵循该 URI。如果响应不包含 next_href 属性,则已到达结果的末尾。

try {
    $tracks = [];
    $params = ['linked_partitioning' => true, 'limit' => 100 ];
    $response = \Soundcloud::get('users/{CLIENT_ID}/tracks', $params);
    while (property_exists($response, 'next_href') && !empty($response->next_href)) {
        $tracks = array_merge($tracks,$response->collection);
        $response = \Soundcloud::get($response->next_href);
    }
} catch (Exit $e) {
    exit($e->getMessage());
}

POST

try {
    $response = \Soundcloud::post(
        'tracks/1/comments',
        [
            'body' => 'a new comment'
        ]
    );
} catch (Exception $e) {
    exit($e->getMessage());
}

PUT

try {
    $response = \Soundcloud::put(
        'tracks/1',
        [
            'title' => 'my new title'
        ]
    );
} catch (Exception $e) {
    exit($e->getMessage());
}

DELETE

try {
    $response = \Soundcloud::delete('tracks/1');
} catch (Exception $e) {
    exit($e->getMessage());
}