📜 ⬆️ ⬇️

Last.fm Scrobbling

Beginning of work


There is no normal library in the pub to work with the Last.FM "Submission" PHP API, I had to figure it out and write myself ...
First you need to go to the registration page of your API account .
After entering all the data, you will be provided with 2 keys: public and secret.
image

image
All fields are required:

We communicate with Last.FM


Now you need to write to submissions@last.fm for you to activate your own clientID.
In principle, it is not necessary to have your own clientID, but if you want to listen to the track, the application through which the track is played is shown, then you still have to write.

image
Here is a little funny log of my correspondence:
Me: Hello.
I want to get my own clientID for use in my queries.
Site - vpleer.ru music search. I want to fasten scrobbling on last.fm
thank
Adrian: Hey there,
I’m afraid to speak English.
Regards, Adrian
Me: Okey, sorry for bad english..Hello.
I own clientID to use in my queries.
Website - music search engine. I want to tie scrobbling to last.fm
Thanks. Understand me?)
Adrian: Hey there,
OK, I understood it this time. Your new client id is "***" - you should be able to start using this. Happy scrobbling!
Regards, Adrian

So here =) I preferred to hide my clientID. If there are any problems with requests from my client ID, they can cover up. (At least that's what they say in the API)
')

Getting Started


Your user should follow the link: www.lastfm.ru/api/auth?api_key= MY_PUBLIC_KEY , where MY_PUBLIC_KEY is your public key.
Further, if the user agrees to work with this application, then it redirects to that same Callback URL with one single GET request 'token' ...

<?php
/*
Coded by Isis (c) 2010
Link: www.lastfm.ru/api/auth?api_key=8ea0be38326c1275db95968ab677cfb6
*/
define( 'API_KEY' , '8ea0be38326c1275db95968ab677cfb6' );
define( 'API_SECRET_KEY' , 'b95968ab6775f7474a4db827be6bcf' );
define( 'CLIENT_ID' , 'tst' ); //Paste your clientID
define( 'CLIENT_VERSION' , '1.0' );

function xml2arr($xml, $recursive = false )
{
if (!$recursive) $array = simplexml_load_string($xml); else $array = $xml ;

$newArray = array() ;
$array = (array)$array ;
foreach ($array as $key =>$ value )
{
$ value = (array)$ value ;
if (isset($ value [0])) $newArray[$key] = trim($ value [0]); else $newArray[$key] = xml2arr($ value , true );
}
return $newArray ;
}

function loginLastFM($url, $type, $post = null )
{
if ($ch = curl_init($url))
{
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, 'Vpleer.ru Scrobbler.' );
$type = $type == 'get' ? curl_setopt($ch, CURLOPT_POST, 0) : curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_REFERER, 'http://www.lastfm.ru/api/' );
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec($ch);
curl_close($ch);
return $content;
}
else
{
return 'notconnect' ;
}
}

//1st step. Get token from $_GET['token']
function getKey($token, $API_KEY, $API_SECRET_KEY)
{
$api_sig = md5( 'api_key' .$API_KEY. 'methodauth.getSessiontoken' .$token.$API_SECRET_KEY);
$ get = 'method=auth.getSession&api_key=' .$API_KEY. '&token=' .$token. '&api_sig=' .$api_sig;
$ return = xml2arr(loginLastFM( 'http://ws.audioscrobbler.com/2.0/' , 'get' , $ get ));
return $ return ;
}

//handShake. . -
function handShake($user, $key, $time, $CLIENT_ID, $CLIENT_VERSION, $API_KEY, $API_SECRET_KEY)
{
$handtoken = md5($API_SECRET_KEY.$time);
$handget = 'hs=true&p=1.2.1&c=' .$CLIENT_ID. '&v=' .$CLIENT_VERSION. '&u=' .$user. '&t=' .$time. '&a=' .$handtoken. '&api_key=' .$API_KEY. '&sk=' .$key;
$handshake = loginLastFM( 'http://post.audioscrobbler.com/' , 'get' , $handget);
$handecho = explode( "\n" , $handshake);
return $handecho;
}

// last.fm
function nowPlaying($session, $artist, $song, $duration)
{
$playget = 's=' .$session. '&a=' .$artist. '&t=' .$song. '&b=&l=' .$duration. '&n=&m=' ;
$playnow = loginLastFM( 'http://post.audioscrobbler.com:80/np_1.2' , 'post' , $playget);
return $playnow;
}

// last.fm 50%
function submission($session, $artist, $song, $duration, $starttime)
{
$subget = 's=' .$session. '&a[0]=' .$artist. '&t[0]=' .$song. '&i[0]=' .$starttime. '&o[0]=P&r[0]=&l[0]=' .$duration. '&b[0]=&n[0]=&m[0]=' ;
$submission = loginLastFM( 'http://post2.audioscrobbler.com:80/protocol_1.2' , 'post' , $subget);
return $submission;
}

function doShake($fmuser, $fmkey, $time, $CLIENT_ID, $CLIENT_VERSION, $API_KEY, $API_SECRET_KEY)
{
$handshake = handShake($fmuser, $fmkey, $time, $CLIENT_ID, $CLIENT_VERSION, $API_KEY, $API_SECRET_KEY);
$handerror = trim($handshake[0]);
$session = trim($handshake[1]);
if ($handerror == 'OK' && isset($session))
{
setcookie( 'fmsess' , $session, time() + 3600 * 24 * 730, '/' , '.' .$_SERVER[ 'HTTP_HOST' ]);
return 'OK' ;
}
else
{
return 'Error : ' .$handerror;
}
}

$time = time();

// last.fm,
if (isset($_GET[ 'token' ]))
{
$ return = getKey($_GET[ 'token' ], API_KEY, API_SECRET_KEY);
$error = isset($ return [ 'error' ]) ? $ return [ 'error' ] : null ;
$key = isset($ return [ 'session' ][ 'key' ]) ? $ return [ 'session' ][ 'key' ] : null ;
$user = isset($ return [ 'session' ][ 'name' ]) ? $ return [ 'session' ][ 'name' ] : null ;
if (!isset($error) && isset($key) && isset($user))
{
setcookie( 'fmkey' , $key, time() + 3600 * 24 * 730, '/' , '.' .$_SERVER[ 'HTTP_HOST' ]);
setcookie( 'fmuser' , $user, time() + 3600 * 24 * 730, '/' , '.' .$_SERVER[ 'HTTP_HOST' ]);
setcookie( 'scrobb' , 'on' , time() + 3600 * 24 * 730, '/' , '.' .$_SERVER[ 'HTTP_HOST' ]);
$a = doShake($user, $key, $time, CLIENT_ID, CLIENT_VERSION, API_KEY, API_SECRET_KEY);
header( 'Location: /scrobb/' );
}
else
{
echo $error;
}
}

//1 ?
if (isset($_COOKIE[ 'fmkey' ], $_COOKIE[ 'fmuser' ]) && !isset($_COOKIE[ 'fmsess' ]) && (isset($_POST[ 'nowplaying' ]) || isset($_POST[ 'submission' ])))
{
$a = doShake($_COOKIE[ 'fmuser' ], $_COOKIE[ 'fmkey' ], $time, CLIENT_ID, CLIENT_VERSION, API_KEY, API_SECRET_KEY);
echo $a;
}

// !
if (isset($_POST[ 'nowplaying' ], $_COOKIE[ 'fmkey' ], $_COOKIE[ 'fmuser' ], $_COOKIE[ 'fmsess' ]))
{
$artist = isset($_POST[ 'artist' ]) ? urldecode($_POST[ 'artist' ]) : 'Undefined' ;
$song = isset($_POST[ 'song' ]) ? urldecode($_POST[ 'song' ]) : 'Undefined' ;
$duration = isset($_POST[ 'duration' ]) ? urldecode($_POST[ 'duration' ]) : 'Undefined' ;
setcookie( 'fmtime' , $time, time() + 600, '/' , '.' .$_SERVER[ 'HTTP_HOST' ]);
echo $playnow = nowPlaying($_COOKIE[ 'fmsess' ], $artist, $song, $duration);
if (!strstr($playnow, 'OK' ))
{
echo doShake($_COOKIE[ 'fmuser' ], $_COOKIE[ 'fmkey' ], $time, CLIENT_ID, CLIENT_VERSION, API_KEY, API_SECRET_KEY);
//$playnow = nowPlaying($_COOKIE['fmsess'], $artist, $song, $duration);
}
}

// last.fm
if (isset($_POST[ 'submission' ], $_COOKIE[ 'fmkey' ], $_COOKIE[ 'fmuser' ], $_COOKIE[ 'fmsess' ]))
{
$artist = isset($_POST[ 'artist' ]) ? urldecode($_POST[ 'artist' ]) : 'Undefined' ;
$song = isset($_POST[ 'song' ]) ? urldecode($_POST[ 'song' ]) : 'Undefined' ;
$duration = isset($_POST[ 'duration' ]) ? urldecode($_POST[ 'duration' ]) : 'Undefined' ;
$starttime = isset($_COOKIE[ 'fmtime' ]) ? $_COOKIE[ 'fmtime' ] : time();
echo $submiss = submission($_COOKIE[ 'fmsess' ], $artist, $song, $duration, $starttime);
if (!strstr($submiss, 'OK' ))
{
echo doShake($_COOKIE[ 'fmuser' ], $_COOKIE[ 'fmkey' ], $time, CLIENT_ID, CLIENT_VERSION, API_KEY, API_SECRET_KEY);
//$submiss = submission($_COOKIE['fmsess'], $artist, $song, $duration, $starttime);
}
}


* This source code was highlighted with Source Code Highlighter .

Description


Now, in order to scrape the track on last.fm at the beginning of playback, you need to transfer the following $ _POST parameters to this script:

And to completely zaskrobblit track, then these parameters:

Download scrobb.php

Source: https://habr.com/ru/post/86940/


All Articles