vodka2/vk-audio-token

Music URL 404 (I don't know if is a issue)

lannodev opened this issue · 3 comments

I'm trying to get artist page using:

//Credentials obtained by example_vkofficial.php script
define('TOKEN', "myToken");
define('USER_AGENT', SupportedClients::VkOfficial()->getUserAgent());
$ch = curl_init();

curl_setopt($ch, CURLOPT_HTTPHEADER, array('User-Agent: ' . USER_AGENT));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);

/* Artisit*/
curl_setopt(
    $ch,
    CURLOPT_URL,
    "https://api.vk.com/method/catalog.getAudioArtist"
);

curl_setopt(
    $ch,
    CURLOPT_POSTFIELDS,
    "v=5.116" . // Api Version
        "&https=1" .
        "&ref=search" .
        "&extended=1" .
        "&lang=pt-br" .
        "&count=5" .
        "&need_blocks=0" .
        "&artist_id=mkjay" .
        "&access_token=" . TOKEN
);


$artist = json_decode(curl_exec($ch));
$section_id = $artist->response->catalog->sections[0]->id;
$artist_title = $artist->response->catalog->sections[0]->title;

if ($artist_title != "") {
    curl_setopt(
        $ch,
        CURLOPT_URL,
        "https://api.vk.com/method/catalog.getSection"
    );

    curl_setopt(
        $ch,
        CURLOPT_POSTFIELDS,
        "v=5.116" . // Api Version
            "&https=1" .
            "&ref=search" .
            "&extended=1" .
            "&lang=en" .
            "&count=5" .
            "&start_from=0" .
            "&next_from=1" .
            "&need_blocks=0" .
            "&section_id=" . $section_id .
            "&access_token=" . TOKEN
    );

    $data = json_decode(curl_exec($ch));

    /* Example of getting mp3 from m3u8 url */
    function getMp3FromM3u8($url)
    {
        // Not a m3u8 url
        if (!strpos($url, "index.m3u8?")) {
            return $url;
        }
        if (strpos($url, "/audios/")) {
            return preg_replace('~^(.+?)/[^/]+?/audios/([^/]+)/.+$~', '\\1/audios/\\2.mp3', $url);
        } else {
            return preg_replace('~^(.+?)/(p[0-9]+)/[^/]+?/([^/]+)/.+$~', '\\1/\\2/\\3.mp3', $url);
        }
    }

    $allAudios = $data->response->audios;
    foreach ($allAudios as $audio) {
        $audio->url = getMp3FromM3u8($audio->url);
    }

    /* Response with mp3 urls */
    echo json_encode($data, JSON_PRETTY_PRINT) . "\n\n";
}

When I run this code on local php server everything its good!
but when i try to run this code in hostinger server (hostinger.com)
the URL audio give to me 404 page

Example response local:

"is_licensed": true,
"track_code": "28646071UzEmKpXVuGVFnfNJbLs8QoTdes4kxHXlBhZuKHhMAaKD_rJD",
"url": "https://cs9-13v4.vkuseraudio.net/p1/9698db4d88d42e.mp3",
"date": 1570532915,

Example response Hostinger:

"is_licensed": true,
"track_code": "27f63f55WbxlxswQK8Yp535KdYNFJEILQJ3SyOVQFLzAvxmdbn-Nc_Gv",
"url": "https://cs9-13v4.vkuseraudio.net/p1/089fd329d9d28d.mp3",
"date": 1570532915,

The Host Server location is EUA
Why this happens ?
All the best!

@luciano-work Are you trying to open mp3 URL that was obtained from Hostinger IP from a local IP? It is a well-known restriction, these URLs are bound to IP.

@vodka2
This is my curl_getinfo($ch) from hostinger and localhost:

{
"url": "https://api.vk.com/method/catalog.getSection",
"content_type": "application/json; charset=utf-8",
"http_code": 200,
"header_size": 252,
"request_size": 217,
"filetime": -1,
"ssl_verify_result": 0,
"redirect_count": 0,
"total_time": 0.633727,
"namelookup_time": 0.000087,
"connect_time": 0.000087,
"pretransfer_time": 0.000167,
"size_upload": 278,
"size_download": 73740,
"speed_download": 116492,
"speed_upload": 439,
"download_content_length": 73740,
"upload_content_length": 278,
"starttransfer_time": 0.00017,
"redirect_time": 0,
"redirect_url": "",
"primary_ip": "87.240.129.140",
"certinfo": [],
"primary_port": 443,
"local_ip": "31.170.166.xxx",
"local_port": 22058,
"http_version": 3,
"protocol": 2,
"ssl_verifyresult": 0,
"scheme": "HTTPS",
"appconnect_time_us": 87,
"connect_time_us": 87,
"namelookup_time_us": 87,
"pretransfer_time_us": 167,
"redirect_time_us": 0,
"starttransfer_time_us": 170,
"total_time_us": 633727
}

Localhost (php -S localhost:8080)

{
"url": "https://api.vk.com/method/catalog.getSection",
"content_type": "application/json; charset=utf-8",
"http_code": 200,
"header_size": 253,
"request_size": 262,
"filetime": -1,
"ssl_verify_result": 0,
"redirect_count": 0,
"total_time": 1.492954,
"namelookup_time": 0.00004,
"connect_time": 0.00004,
"pretransfer_time": 0.000097,
"size_upload": 286,
"size_download": 134905,
"speed_download": 90418,
"speed_upload": 191,
"download_content_length": 134905,
"upload_content_length": 286,
"starttransfer_time": 0.000102,
"redirect_time": 0,
"redirect_url": "",
"primary_ip": "87.240.139.193",
"certinfo": [],
"primary_port": 443,
"local_ip": "192.168.2.106",
"local_port": 50998,
"http_version": 3,
"protocol": 2,
"ssl_verifyresult": 0,
"scheme": "HTTPS",
"appconnect_time_us": 40,
"connect_time_us": 40,
"namelookup_time_us": 40,
"pretransfer_time_us": 97,
"redirect_time_us": 0,
"starttransfer_time_us": 102,
"total_time_us": 1492954
}

You think that the problem is "local_ip": ?
All the best!

@luciano-work yes, the problem is with the local_ip. Maybe my explanation was not clear, I mean that when you query audio URLs from one IP (31.170.166.xxx) you can't access them from other IPs, including your local IP. It was so even when VK API was public.