Curl with Json - Eg: parameters":{"ch":false,"mo":false,"country":{"ID":228

Discussion in 'PHP' started by ps3ubo, Apr 8, 2010.

  1. #1
    Hey
    How to I do this with CURL (Post this info):
    {"header":{"token":"637c66ba13e277d404ccbc9067d2a0bc252798414fe3fd","clientRevision":"20100323.02","uuid":"4CAA55AC-0DA2-B664-6B66-DCFB6BA137D4","country":{"ID":223,"CC3":"0","CC1":"0","CC4":"2147483648","CC2":"0"},"client":"widget","session":"e0f2bc1f1285da022ce980bfbaa4d4b3"},"parameters":{"prefetch":false,"mobile":false,"country":{"ID":223,"CC3":"0","CC1":"0","CC4":"2147483648","CC2":"0"},"songID":10508786},"method":"getStreamKeyFromSongIx"}

    This is what I get from my POSTDATA stuff :S
     
    ps3ubo, Apr 8, 2010 IP
  2. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #2
    lukeg32, Apr 8, 2010 IP
  3. ps3ubo

    ps3ubo Peon

    Messages:
    204
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks! thats what I need but i now tried it and it doesn't work for me ;( not the json thing but the source i am trying to view is been silly with me :confused:
     
    ps3ubo, Apr 8, 2010 IP
  4. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #4
    What are you trying to do? I.E What are you posting to, and what data are you posting?

    Are you getting any errors returned from the script or from the remote server at all?
     
    lukeg32, Apr 8, 2010 IP
  5. ps3ubo

    ps3ubo Peon

    Messages:
    204
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hehe, I'm trying to make a script to access Groovesharks remote files :) But it says something about wrong method type.. but the method type is correct as the page cache shows it connects to that :confused:
     
    ps3ubo, Apr 8, 2010 IP
  6. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #6
    lukeg32, Apr 8, 2010 IP
  7. ps3ubo

    ps3ubo Peon

    Messages:
    204
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    ps3ubo, Apr 8, 2010 IP
  8. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #8
    Well, I was just looking into this but that wiki has the following warning against it;

    " Using the information below to communicate with Grooveshark servers is against their TOS "

    So, perhaps a method has changed or you are passing it incorrectly .... im not sure, but im not going to create my own script to do this :)
     
    lukeg32, Apr 8, 2010 IP
  9. ps3ubo

    ps3ubo Peon

    Messages:
    204
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Okey :) I didn't read there TOS :p
     
    ps3ubo, Apr 8, 2010 IP
  10. adammw

    adammw Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Hi.
    I'm the admin/creator of the nettech wiki, and I'm sorry that my wiki wasn't clear to you. If you could tell me which bits need improving I'll see what I can do.

    For starters, it looks like your using the gadget code. There's nothing wrong with that except that the methods I described come from the full "listen.grooveshark.com" client, so they might not be fully compatible.

    Now to your specific question, this is how I make the requests (as I also use PHP/cURL):
    
    //First I get the PHP session ID from Grooveshark
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://listen.grooveshark.com/');
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $page = curl_exec($ch);
    curl_close($ch);
    if (!preg_match('/sessionID: \'([0-9a-f]+?)\',/',$page,$matches)) {
    	die('Session ID could not be retrieved');
    } else {
    	
    	//Then I call getCommunicationToken to get a token (note that the sessionID i got before is used here)
    	$data['header'] = Array('client'=>'gslite', 'uuid'=>'489B126D-61D2-0BA7-7C59-24811C5F65A3', 'session'=>$matches[1], 'clientRevision'=>file_get_contents('flashRev.txt')); //you need to replace file_get_contents('flashRev.txt') with the latest flash revision, so far it's 20100412.02
    	$data['parameters'] = Array('secretKey' => md5($matches[1]));
    	$data['method'] = 'getCommunicationToken';
    	
    	$ch = curl_init();
    	curl_setopt($ch, CURLOPT_URL, 'https://cowbell.grooveshark.com/service.php');
    	curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    	curl_setopt($ch, CURLOPT_POST, 1);
    	curl_setopt($ch, CURLOPT_COOKIE, "PHPSESSID={$matches[1]}");
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    	curl_setopt($ch, CURLOPT_HEADER, 0);
    	curl_setopt($ch, CURLOPT_HTTPHEADER,Array('Referer: http://listen.grooveshark.com/main.swf?cowbell=120918','Content-Type: application/json','User-Agent: Mozilla/4.0 (compatible; Windows NT 5.1) GroovesharkClient/0.1','x-flash-version: 10,0,32,18'));
    	$response = json_decode(curl_exec($ch),true);
    	curl_close($ch);
    
    	// Then we do some basic checking to see if that was succesful
    	if(isset($response['fault'])) {
    	if ($response['fault']['message'] == ' invalid client') {
    			die('This script is out-of-date. Please update the clientRevision parameter in this script.');
    		} else {
    			die('Fatal error.');
    		}
    	}
    	
    	// Otherwise, we just keep the result (which is the token fragment)
    	$token = $response['result'];
    
    	//Now that we have the token *fragment*, we can actually make up the request.
    	// I see that you already had a token, but if that was obtained from a simple packet capture
    	// it will not work, as it will have expired already. This code should work, as it uses a new session 
    	// and fetches a new token fragment. It is important to note that more processing needs to be done and this cannot be simply sent as the token parameter
    
    	// This bit generates the payload
    	$data['method'] = 'getStreamKeyFromSong';
    	$data['header']['token'] = genToken($data['method'],$token); //see the function refrence below
    	$data['parameters'] = Array('prefetch' => false, 'songID' => $songid); //note that you need a songID
    	$ch = curl_init();
    	curl_setopt($ch, CURLOPT_URL, 'https://cowbell.grooveshark.com/more.php?'.$data['method']);
    	curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    	curl_setopt($ch, CURLOPT_POST, 1);
    	curl_setopt($ch, CURLOPT_COOKIE, "PHPSESSID=$session");
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    	curl_setopt($ch, CURLOPT_HEADER, 0);
    	curl_setopt($ch, CURLOPT_HTTPHEADER,Array('Referer: http://listen.grooveshark.com/main.swf?cowbell=120918','Content-Type: application/json','User-Agent: Mozilla/4.0 (compatible; Windows NT 5.1) GroovesharkClient/0.1','x-flash-version: 10,0,32,18'));
    	$response = json_decode(curl_exec($ch),true);
    	curl_close($ch);
    
    	//At this point you will have a result (hopefully), which includes
    	// $response['result']['result']['streamServer'] - which is the server you need to POST to
    	// $response['result']['result']['streamKey'] - the key you need to use
    	// e.g. you might need to post to http://stream36.grooveshark.com/stream.php the following data (content-type: application/x-www-form-urlencoded):
    	// streamKey=abcdef12351123
    }
    
    // This function generates the tokens you need
    function genToken ($method, $token) 
    {
    	$randomKey = substr(uniqid(),-6);
    	return $randomKey . sha1($method . ':' . $token . ':'. file_get_contents('salt.txt').':' . $randomKey);
    	// you will need to replace file_get_contents('salt.txt') with the string 'quitStealinMahShit', but this may change
    }
    
    
    
    PHP:
    Sorry that it's so ugly, and probably slow, but it works...
    Either email me, PM me or reply if you need more help!
     
    adammw, Apr 18, 2010 IP
  11. Ulquiorra

    Ulquiorra Peon

    Messages:
    422
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Ulquiorra, Apr 21, 2010 IP
  12. adammw

    adammw Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    It depends on what he is trying to do... he might not want that exact functionality.

    I don't like the idea of people making money off someone else's service... Therefore, if anyone would like my source code (which does the same thing) email me and I'll send it to you for no cost, however I would like to note that using Grooveshark for the primary purpose of "stealing" music is wrong and of course against their TOS, please use code like this only for creating better clients or search-based web apps. Demo at http://wordpress.adammw111.co.cc/projects/grooveshark-music-client/ .
     
    adammw, Apr 21, 2010 IP
  13. Ulquiorra

    Ulquiorra Peon

    Messages:
    422
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #13
    I'd give away the code for free but that wouldn't be fair to people who already bought it. Not to mention I originally made the code for an android app way back when grooveshark didn't have it's own.

    In any case, if you wanna know how grooveshark does it, check out: http://grooveshark.com/wordpress

    Download it and look at the source for some tips.
     
    Ulquiorra, Apr 22, 2010 IP
  14. kaputtmacher

    kaputtmacher Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    @adammw
    your code is not working anymore, the communicationToken is broken (has about 12 chars, should be 32?)
    if anyone could tell me what has changed.. would be nice (want to develop it in java but get the same error as your php code)
     
    kaputtmacher, May 25, 2010 IP