【发布时间】:2022-01-13 15:30:48
【问题描述】:
我一直在尝试使用几个 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 文件时是否出现了语法或正则表达式错误,或者第二个文件是否因为字符串的其他元素而无法正常工作?
提前致谢。
【问题讨论】:
-
我想你想
include("extract.php")而不是file_get_contents("extract.php")。include()函数运行文件,而file_get_contents()只返回文件的内容。 -
感谢您的回答!我制作了一个新脚本并将
file_get_contents替换为include,但现在它返回的内容与extract.php相同,而不仅仅是m3u8 文件。我认为第二个脚本无法识别 url,因为它位于字符串的中间。 -
你应该发布你的
extract.php,如果没有它,就很难知道发生了什么。 -
extract.php是我在我的问题中发布的第一个脚本,它获取了这个pwn.shPython 脚本生成的信息并以纯文本形式打印出来。也许我应该以某种方式修改它以删除不属于 URL 的所有内容,但我不知道如何。
标签: php curl web-scraping http-live-streaming m3u8