PHP code example of zyan / v2d

1. Go to this page and download the library: Download zyan/v2d library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

zyan / v2d example snippets


use Zyan\V2d\V2d;

$v2d = new V2d();

// 直接传入分享短链,自动识别平台
$result = $v2d->fetch('https://v.douyin.com/coTA27_qFBA/');

// 内容类型:image 或 video
echo $result->getType();      // image
echo $result->getPlatform();  // douyin
echo $result->getTitle();     // 标题
echo $result->getAuthor()->getNickname(); // 作者昵称

if ($result->getType() === 'image') {
    foreach ($result->getImages() as $image) {
        echo $image . PHP_EOL;
    }
} else {
    echo $result->getVideoUrl(); // 最优视频地址
}

// 背景音乐
$music = $result->getMusic();
if (!$music->isEmpty()) {
    echo $music->getTitle(); // 音乐标题
    echo $music->getUrl();   // 音乐播放地址(部分平台因版权可能为空)
}

$array = $v2d->fetchArray('https://v.kuaishou.com/Jdl248ZA');
$json  = $v2d->fetchJson('http://xhslink.com/o/wjWTM5ZvMj');

use Zyan\V2d\Adapters\Douyin\DouyinAdapter;
use Zyan\V2d\Adapters\Kuaishou\KuaishouAdapter;
use Zyan\V2d\Adapters\Xiaohongshu\XiaohongshuAdapter;

$douyin = new DouyinAdapter();
$result = $douyin->fetch('https://v.douyin.com/coTA27_qFBA/');

// 判断适配器是否支持某链接
$douyin->supports('https://v.douyin.com/xxx/'); // true

use Zyan\V2d\Adapters\Xiaohongshu\XiaohongshuAdapter;

$adapter = (new XiaohongshuAdapter())
    ->withCookie('webId=xxx; a1=xxx; web_session=xxx;');

$result = $adapter->fetch('http://xhslink.com/o/wjWTM5ZvMj');

$adapter->withHeaders([
    'User-Agent' => '你的 UA',
    'Referer'    => 'https://www.xiaohongshu.com/',
    'Cookie'     => '...',
]);
// 或整体替换
$adapter->setHeaders(['User-Agent' => '...']);

use GuzzleHttp\Client;
use Zyan\V2d\V2d;

$client = new Client([
    'proxy' => 'http://127.0.0.1:7890',
    'timeout' => 20,
    'verify' => false,
]);

$v2d = new V2d(null, $client);
$result = $v2d->fetch('https://v.kuaishou.com/Jdl248ZA');

use Zyan\V2d\Adapters\AbstractAdapter;
use Zyan\V2d\Results\Result;

class BilibiliAdapter extends AbstractAdapter
{
    public function getPlatform(): string
    {
        return 'bilibili';
    }

    public function supports(string $url): bool
    {
        return (bool) preg_match('#b23\.tv/#i', $url);
    }

    public function fetch(string $url): Result
    {
        // 解析逻辑...
    }
}

$v2d = new V2d();
$v2d->register(new BilibiliAdapter());

use Zyan\V2d\V2d;
use Zyan\V2d\Exceptions\Exception;

try {
    $result = (new V2d())->fetch($url);
} catch (Exception $e) {
    // 统一捕获
    echo $e->getMessage();
}

src/
├── V2d.php                         # 主入口
├── Contracts/
│   └── AdapterInterface.php        # 适配器契约
├── Adapters/
│   ├── AbstractAdapter.php         # 适配器基类(HTTP/JSON 工具)
│   ├── Douyin/DouyinAdapter.php
│   ├── Kuaishou/KuaishouAdapter.php
│   └── Xiaohongshu/XiaohongshuAdapter.php
├── Results/
│   ├── Result.php                  # 结果基类
│   ├── Author.php                  # 作者值对象
│   ├── Music.php                   # 背景音乐值对象
│   ├── ImageResult.php             # 图文结果
│   └── VideoResult.php             # 视频结果
└── Exceptions/
    └── Exception.php               # 异常体系
tests/                              # PHPUnit 测试