我一直在嘗試使用幾個 PHP 腳本從 Twitch 抓取 HLS 檔案。第一個運行 cURL 命令以通過 Python 腳本獲取 HLS URL,該腳本回傳所述 URL 并將生成的字串轉換為純文本,第二個(這是不作業的)假定提取 M3U8 檔案并使其能夠播放。
第一個腳本(extract.php)
<?php
header('Content-Type: text/plain; charset=utf-8');
$url = "https://pwn.sh/tools/streamapi.py?url=twitch.tv/cgtn_live_russian&quality=1080p60";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);
$undesirable = array("}");
$cleanurl = str_replace($undesirable,"");
echo substr($cleanurl, 39, 898);
?>
這個腳本(我們稱之為extract.php)有效,它回傳(以純文本形式)與 Python 腳本回傳的資訊相同的資訊,即:
string(904) "{"success": true, "urls": {"1080p60": "https://video-weaver.fra05.hls.ttvnw.net/v1/playlist/[token].m3u8"}}"
第二個腳本(play.php)
<?php
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Referer:https://myserver.com/" .
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:51.0) Gecko/20100101 Firefox/51.0"
));
$html = file_get_contents("extract.php");
preg_match_all(
'/(http.*?\.m3u8[^&">] )/',
$html,
$posts, // will contain the article data
PREG_SET_ORDER // formats data into an array of posts
);
foreach ($posts as $post) {
$link = $post[0];
header("Location: $link");
}
?>
第二個腳本(我們稱之為play.php)理論上應該回傳 M3U8 檔案(沒有string(904) "{"success": true, "urls": {"1080p60":)并使其能夠在媒體播放器,例如 VLC,但它不回傳任何內容。
有人可以告訴我有什么問題嗎?我在制作這些 PHP 檔案時是否出現了語法或正則運算式錯誤,或者第二個檔案是否因為字串的其他元素而無法正常作業?
提前致謝。
uj5u.com熱心網友回復:
我認為您可以依靠正則運算式來獲取 URL,而不是嘗試手動清理字串。另一種方法是使用json_decode().
無論如何,這個想法是在 中定義一個變數extract.php,在這種情況下它是$resp。按照現在echo的方式進行操作不會使其在父腳本中可用。
然后,您可以在play.php一次extract.php包含該變數時參考該變數。
<?php
//extract.php
$resp = '';
$url = "https://pwn.sh/tools/streamapi.py?url=twitch.tv/cgtn_live_russian&quality=1080p60";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($curl);
curl_close($curl);
//play.php
include('./extract.php');
//$resp is set in extraact.php
preg_match_all(
'/(http.*?\.m3u8)/',
$resp,
$posts, // will contain the article data
PREG_SET_ORDER // formats data into an array of posts
);
foreach ($posts as $post) {
$link = $post[0];
}
header("Location: $link");
die();
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/412066.html
標籤:
