noweh/php-soundcloud

Soundcloud API包装器。

1.0.0 2021-10-27 15:06 UTC

This package is auto-updated.

Last update: 2024-09-20 00:25:19 UTC


README

SoundCloud PHP MIT Licensed last version

一个用于SoundCloud REST API端点的PHP包装器。

安装

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

composer require noweh/php-soundcloud

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

使用方法

为了使调用有效,您必须遵循以下步骤

首先,您必须使用以下参数创建包装器的新实例

use Noweh\SoundcloudApi\Soundcloud;

$client = new SoundCloud(
    {CLIENT_ID},
    {CLIENT_SECRET},
    {CALLBACK_URL}
);

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

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

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

...
header("Location: " . $client->getAuthorizeUrl('a_custom_param_to_retrieve_in_callback'));
exit();

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

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

use Noweh\SoundcloudApi\Soundcloud;

$client = new SoundCloud(
    {CLIENT_ID},
    {CLIENT_SECRET},
    {CALLBACK_URL}
);

$client->setCode('3-134981-158678512-IwAXqypKWlDJCF');

// API Call
...

获取播放器嵌入

此调用不需要access_token。

要获取指向用户、设置或播放列表的任何SoundCloud URL的widget嵌入代码,请执行以下操作

... // Create a new instance of client

// 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 = $client->getPlayerEmbed($url, $maxheight, $sharing, $liking, $download, $show_comments, $show_playcount, $show_user)
} catch (Exception $e) {
    exit($e->getMessage());
}

GET

... // Create a new instance of client

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

POST

... // Create a new instance of client

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

PUT

... // Create a new instance of client

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

DELETE

... // Create a new instance of client

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