Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Spotify Web Api Php Laravel Package

jwilsson/spotify-web-api-php

View on GitHub
Deep Wiki
Context7

Passing a Custom Request Instance

Sometimes you want to pass a custom Request instance to SpotifyWebAPI. For example to use another HTTP library or provide additional logging.

This is possible by extending the Request class and providing your own send method.

class MyRequest extends SpotifyWebAPI\Request
{
    public function send(string $method, string $url, string|array|object $parameters = [], array $headers = []): array
    {
        // Do your thing here

        // But be sure to set the lastResponse property for other parts to work correctly
        $this->lastResponse = [
            'body' => $body, // The JSON response body parsed to a PHP value
            'headers' => $headers, // An array of the headers returned
            'status' => $status, // The HTTP response status code
            'url' => $url, // The requested URL
        ];

        return $this->lastResponse;
    }
}

Then when you wish to use it, pass it to the SpotifyWebAPI and Session constructors.

$request = new MyRequest();

$session = new SpotifyWebAPI\Session(
    'CLIENT_ID',
    'CLIENT_SECRET',
    'REDIRECT_URI',
    $request
);

$api = new SpotifyWebAPI\SpotifyWebAPI(
    $options,
    $session,
    $request
);

Helper functions

The Request class provides a few helper functions that can be useful when working with the responses from Spotify.

handleResponseError

protected function handleResponseError(string $body, int $status): void

Takes the unparsed response body and tries to figure out what kind of error occured in order to provide some additional info in the error thrown.

parseBody

protected function parseBody(string $body): mixed

Takes the unparsed response body and parses it, taking the return_assoc option into account.

parseHeaders

protected function parseHeaders(string $headers): array

Takes the HTTP header block, parses it to a key-value array while normalizing header names. If you're using an external HTTP library it will most definitely already include a method for this.

splitResponse

protected function splitResponse(string $response): array

Takes the full HTTP response and splits it into headers and body while stripping additional headers sometimes added by proxy servers.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor
spatie/laravel-javascript-views