danae / soundcloud-php
PHP的SoundCloud API包装器,易于使用
3.0.1
2022-03-04 20:23 UTC
Requires
- php: ^7.4||^8.0
- guzzlehttp/guzzle: ~6.0
This package is auto-updated.
Last update: 2024-09-05 01:50:32 UTC
README
soundcloud-php 是PHP 7.4或更高版本的SoundCloud® API包装器。
安装
建议使用Composer安装SoundCloud库
$ composer require danae/soundcloud-php
或者,在composer.json的require块中包含以下行
"require": { "danae/soundcloud-php": "^3.0" }
使用方法
以下是库的使用示例
require("vendor/autoload.php"); // Create the client $soundcloud = new Danae\Soundcloud\Soundcloud([ 'client_id' => '<client id>', 'client_secret' => '<client secret>', 'redirect_url' => '<redirect url>' ]); // Authorize the client with an authorization code $soundcloud->authorizeWithCode('<authorization code>'); // Get the tracks of the authenticated user $tracks = $soundcloud->get('/me/tracks');
要使用库,您必须创建一个Danae\SoundCloud\Soundcoud
类的实例,并将client_id和client_secret提供给构造函数
$soundcloud = new Danae\Soundcloud\Soundcloud([ 'client_id' => '<client id>', 'client_secret' => '<client secret>' ]);
授权
要使用客户端,您必须先对其进行认证。客户端支持SoundCloud API提供的auth_code
、client_credentials
和refresh_token
授权。您可以使用以下方法之一对客户端进行认证
// Using an authorization code $soundcloud->authorizeWithCode('<authorization code>'); // Using client credentials provided via the constructor $soundcloud->authorizeWithClientCredentials(); // Using a refresh token $soundcloud->authorizeWithRefreshToken('<refresh token>');
授权成功后,访问令牌将自动添加到客户端的请求中,但也可以通过客户端实例获取。
$accessToken = $soundcloud->accessToken;
使用授权代码进行授权
将至少包含client_id
、client_secret
和redirect_uri
键提供给客户端构造函数。然后调用authorizeWithCode
方法。
$soundcloud = new Danae\Soundcloud\Soundcloud([ 'client_id' => '<client id>', 'client_secret' => '<client secret>', 'redirect_url' => '<redirect url>' ]); $soundcloud->authorizeWithCode('<authorization code>');
使用客户端凭据进行授权
将至少包含client_id
和client_secret
键提供给客户端构造函数。然后调用authorizeWithClientCredentials
方法。
$soundcloud = new Danae\Soundcloud\Soundcloud([ 'client_id' => '<client id>', 'client_secret' => '<client secret>' ]); $soundcloud->authorizeWithClientCredentials();
使用刷新令牌进行授权
$soundcloud = new Danae\Soundcloud\Soundcloud([ 'client_id' => '<client id>', 'client_secret' => '<client secret>', 'redirect_url' => '<redirect url>' ]); $soundcloud->authorizeWithRefreshToken('<refresh_token>');
发送请求
客户端授权后,您可以使用以下方法向SoundCloud API发送请求并接收由json_decode
创建的stdClass
对象。所有请求函数在请求失败时都会抛出一个带有状态码的Runtimexception
。
// Send a request to one of the API endpoints $soundcloud->get($uri, array $query = []); $soundcloud->post($uri, array $body = [], array $query = []); $soundcloud->put($uri, array $body[], array $query = []); $soundcloud->delete($uri, array $query = []); // Resolve a SoundCloud URL to an API endpoint $soundcloud->resolve($url, array $query = []); // Send an oembed request to the API $soundcloud->oembed($url, array $query = []);
有关API本身的更多信息,请参阅SoundCloud API探索器或相关指南。