Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wbi 签名 PHP 类库 #813

Closed
imPrk0 opened this issue Sep 12, 2023 · 1 comment · Fixed by #866
Closed

Wbi 签名 PHP 类库 #813

imPrk0 opened this issue Sep 12, 2023 · 1 comment · Fixed by #866
Labels
新增/Add 添加或修改新的内容 想法/Idea 提供全新的观点
Milestone

Comments

@imPrk0
Copy link

imPrk0 commented Sep 12, 2023

文件在 /docs/misc/sign/wbi.md

PHP 例子:

<?php
/**
 * B站 Wbi 测试
 * @author Prk
 */


class fuckBilibili {

    private $mixinKeyEncTab = [
        46, 47, 18, 2, 53, 8, 23, 32, 15, 50, 10, 31, 58, 3, 45, 35, 27, 43, 5, 49,
        33, 9, 42, 19, 29, 28, 14, 39, 12, 38, 41, 13, 37, 48, 7, 16, 24, 55, 40,
        61, 26, 17, 0, 1, 60, 51, 30, 4, 22, 25, 54, 21, 56, 59, 6, 63, 57, 62, 11,
        36, 20, 34, 44, 52
    ];

    function __construct() {
    }

    public function reQuery(array $query) {
        $wbi_keys = $this->getWbiKeys();
        return $this->encWbi($query, $wbi_keys['img_key'], $wbi_keys['sub_key']);
    }

    private function getMixinKey($orig) {
        $t = '';
        foreach ($this->mixinKeyEncTab as $n) $t .= $orig[$n];
        return substr($t, 0, 32);
    }

    private function encWbi($params, $img_key, $sub_key) {
        $mixin_key = $this->getMixinKey($img_key . $sub_key);
        $curr_time = time();
        $chr_filter = "/[!'()*]/";

        $query = [];
        $params['wts'] = $curr_time;

        ksort($params);

        foreach ($params as $key => $value) {
            $value = preg_replace($chr_filter, '', $value);
            $query[] = urlencode($key) . '=' . urlencode($value);
        }

        $query = implode('&', $query);
        $wbi_sign = md5($query . $mixin_key);

        return $query . '&w_rid=' . $wbi_sign;
    }

    private function getWbiKeys() {
        $resp = @json_decode(
            $this->curl_get(
                'https://api.bilibili.com/x/web-interface/nav',
                null,
                'https://www.bilibili.com/'
            ), true
        );

        if (!$resp) throw new Exception('请求失败');

        $img_url = $resp['data']['wbi_img']['img_url'];
        $sub_url = $resp['data']['wbi_img']['sub_url'];

        return [
            'img_key' => substr(basename($img_url), 0, strpos(basename($img_url), '.')),
            'sub_key' => substr(basename($sub_url), 0, strpos(basename($sub_url), '.'))
        ];
    }

    private function curl_get($url, $cookies = null, $referer = 'https://www.bilibili.com/', $ua = null, $proxy = null, $header = []) {
        $ch = curl_init();
        $header[] = "Accept: */*";
        $header[] = "Accept-Language: en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7";
        $header[] = "Connection: close";
        $header[] = "Cache-Control: max-age=0";
        curl_setopt_array($ch, [
            CURLOPT_HTTPGET         =>  1,
            CURLOPT_CUSTOMREQUEST   =>  'GET',
            CURLOPT_RETURNTRANSFER  =>  1,
            CURLOPT_HTTPHEADER      =>  $header,
            CURLOPT_ENCODING        =>  '',
            CURLOPT_URL             =>  $url,
            CURLOPT_USERAGENT       =>  'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36 Edg/102.0.1245.39',
            CURLOPT_TIMEOUT         =>  15
        ]);

        if ($cookies) curl_setopt(
            $ch,
            CURLOPT_COOKIE,
            $cookies
        );

        if ($referer) curl_setopt_array($ch, [
            CURLOPT_AUTOREFERER =>  $referer,
            CURLOPT_REFERER     =>  $referer
        ]);

        $content = curl_exec($ch);
        curl_close($ch);
        return $content;
    }
}

$c = new fuckBilibili();
$url = 'https://api.bilibili.com/x/space/wbi/acc/info?' . $c->reQuery(['mid' => '123171362']);

echo $url . PHP_EOL;

echo PHP_EOL;

$ch = curl_init();
$header = [];
$header[] = "Accept: */*";
$header[] = "Accept-Language: en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7";
$header[] = "Connection: close";
$header[] = "Cache-Control: max-age=0";
curl_setopt_array($ch, [
    CURLOPT_HTTPGET         =>  1,
    CURLOPT_CUSTOMREQUEST   =>  'GET',
    CURLOPT_RETURNTRANSFER  =>  1,
    CURLOPT_HTTPHEADER      =>  $header,
    CURLOPT_ENCODING        =>  '',
    CURLOPT_URL             =>  $url,
    CURLOPT_USERAGENT       =>  'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36 Edg/102.0.1245.39',
    CURLOPT_TIMEOUT         =>  15,
    CURLOPT_AUTOREFERER     =>  'https://space.bilibili.com/123171362',
    CURLOPT_REFERER         =>  'https://space.bilibili.com/123171362'
]);

$content = curl_exec($ch);
curl_close($ch);

echo $content;
@xiaoyv404 xiaoyv404 added 新增/Add 添加或修改新的内容 想法/Idea 提供全新的观点 labels Sep 12, 2023
@xiaoyv404
Copy link
Collaborator

pr welcome

@xiaoyv404 xiaoyv404 added this to the API Update milestone Nov 16, 2023
@catlair catlair mentioned this issue Nov 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
新增/Add 添加或修改新的内容 想法/Idea 提供全新的观点
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants