【问题标题】:How to select specific text from a string generated by a PHP script?如何从 PHP 脚本生成的字符串中选择特定文本?
【发布时间】: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.sh Python 脚本生成的信息并以纯文本形式打印出来。也许我应该以某种方式修改它以删除不属于 URL 的所有内容,但我不知道如何。

标签: php curl web-scraping http-live-streaming m3u8


【解决方案1】:

我认为您可以依靠正则表达式来获取 URL,而不是尝试手动清理字符串。另一种方法是使用json_decode()。

无论如何,我们的想法是在extract.php 中定义一个变量,在本例中为$resp。像现在这样通过echo 执行此操作不会使其在父脚本中可用。

一旦包含extract.php,您就可以在play.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();

【讨论】:

  • 非常感谢你!脚本成功了!它现在抓取 M3U8 文件而不是字符串,谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-16
  • 1970-01-01
  • 2012-09-03
  • 1970-01-01
  • 1970-01-01
  • 2013-09-27
相关资源
最近更新 更多