Advertisement
makapoh

VK API Audio Upload

Aug 29th, 2012
1,046
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.61 KB | None | 0 0
  1. <?php
  2. function api($method, $params = array()) {
  3.     $token = $_SESSION["token"];
  4.     $paramstr = http_build_query($params);
  5.     $url = "https://api.vk.com/method/" . $method . "?" . $paramstr . "&access_token=" . $token;
  6.     echo "query url: " . $url . "<br />\n";
  7.     $ch = curl_init();
  8.     curl_setopt($ch, CURLOPT_HEADER, 0);
  9.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  10.     curl_setopt($ch, CURLOPT_URL, $url);
  11.     $data = curl_exec($ch);
  12.     echo "response:<br />\n" . $data . "<br /><br />\n";
  13.         curl_close($ch);
  14.     return json_decode($data);
  15. }
  16. session_start();
  17.    
  18. if (!isset($_SESSION["token"]))
  19.     header('Location: http://api.vk.com/oauth/authorize?client_id=2642786&scope=audio&redirect_uri=http://home.makarushin.ru/apitest/auth_audio.php&response_type=code');
  20.  
  21.  
  22. if ($_SERVER["REQUEST_METHOD"]=="POST") {
  23.     $r = api("audio.getUploadServer");
  24.     $ch = curl_init($r->response->upload_url);
  25.     curl_setopt($ch, CURLOPT_HEADER, 0);
  26.     curl_setopt($ch, CURLOPT_VERBOSE, 0);
  27.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  28.     curl_setopt($ch, CURLOPT_POST, true);
  29.     curl_setopt($ch, CURLOPT_POSTFIELDS, array("file" => "@test.mp3"));
  30.     curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
  31.     $audio = json_decode(curl_exec($ch));
  32.     curl_close($ch);
  33.     var_dump($audio);
  34.  
  35.     $r = api("audio.save", array(
  36.         "server" => $audio->server,
  37.         "audio" => $audio->audio,
  38.         "hash" => $audio->hash
  39.     ));
  40. }
  41. ?>
  42. <html>
  43. <head>
  44. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  45. <title>API test</title>
  46. </head>
  47. <body>
  48.     <form method="post">
  49.         <input type="submit" value="test" />
  50.     </form>
  51. </body>
  52. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement