在 CMS、社區、聚合平台開發中,常需從視頻頁面 URL 自動提取標題、縮圖、播放器嵌入代碼。VideoUrlParser 是一個輕量 PHP 庫,支援優酷、土豆、騰訊視頻、搜狐、樂視、新浪、56、酷六等主流國內平台,提供統一介面獲取結構化視頻資訊。
<?php
require_once 'VideoUrlParser.class.php';
$url = 'https://v.youku.com/v_show/id_XMjkwMzc0Njg4.html';
$info = VideoUrlParser::parse($url);
if ($info) {
echo "標題:{$info['title']}\n";
echo "縮圖:{$info['img']}\n";
echo "播放頁:{$info['url']}\n";
echo "SWF 嵌入:{$info['swf']}\n";
echo "直接可用嵌入碼:{$info['object']}\n";
} else {
echo "不支援的平台或解析失敗";
}
| 平台 | URL 範例 | 備註 | |
|---|---|---|---|
| 優酷 | https://v.youku.com/v_show/id_Xxxxx.html | 支援播放列表頁 | |
| 騰訊視頻 | https://v.qq.com/x/cover/xxx.html | 支援 vid 提取與多種 URL 格式 | |
| 土豆 | https://www.tudou.com/playlist/p/xxx.html | 處理 GB2312 編碼頁面 | |
| 酷六 | https://v.ku6.com/show/xxx.html | 支援多種頁面結構 | |
| 56網 | https://www.56.com/u68/v_xxx.html | 調用官方 JSON API | |
| 樂視 | https://www.letv.com/ptv/vplay/xxx.html | 提取播放器參數 | |
| 搜狐視頻 | https://tv.sohu.com/xxx | Open Graph 標籤解析 | |
| 新浪視頻 | https://video.sina.com.cn/v/b/xxx.html | 解析複雜 JSON 嵌入結構 |
class VideoUrlParser {
// 統一入口:自動識別平台分發至私有解析方法
static public function parse($url, $createObject = true) {
$domain = self::detectDomain($url);
$method = "_parse{$domain}";
if (method_exists(self::class, $method)) {
$data = self::$method($url);
if ($data && $createObject) {
$data['object'] = self::buildEmbedCode($data['swf']);
}
return $data;
}
return false;
}
// 統一 HTTP 獲取:支援 file_get_contents、cURL、fsockopen 多策略
private static function _fget($url) { /* ... */ }
private static function _cget($url) { /* ... */ }
private static function _fsget($path, $host) { /* ... */ }
// GZIP 解壓支援(應對壓縮回應)
private static function _gzdecode($data) { /* RFC 1952 實現 */ }
}
file_get_contents、fsockopen,提高相容性。iconv('GB2312', 'UTF-8')。zlib 擴展環境。json_decode;其他平台從 HTML 正則提取關鍵參數。buildEmbedCode() 產出標準 <embed> 標籤,前端直接可用。Vendor\VideoUrlParser。<video> / M3U8 / MP4 直鏈,淘汰 Flash SWF 嵌入模式。⚠️ 視頻解析僅供技術學習與個人專案使用。商業應用前請確認:
- 目標平台《服務條款》是否允許第三方抓取/嵌入
- 是否侵犯內容版權(轉碼、去水印、下載分發均屬高風險)
- 建議採用平台官方提供的 oEmbed / API / 分享嵌入代碼
VideoUrlParser 展示了 PHP 在網頁解析、多平台適配、舊版環境相容性方面的工程實力。現代化重構後可成為內容聚合平台的核心基礎設施之一。