我可以编辑脚本以根据网址/字段输入重新运行吗?

时间:2017-03-02 02:43:09

标签: php regex web-scraping

我有一个脚本,可以根据外部html页面获取流链接。

有没有办法让一个脚本和一个资源页面包含指向所有外部链接的链接,还是每个请求都需要一个单独的页面?

换句话说,我可以这样做吗

example.com/videos.php?v=1234

或者我必须使用

example.com/eachVideoPage.php

这是我用来获取链接的脚本。

<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/xmpegURL");


?>
<?php
ob_start(); // ensures anything dumped out will be caught
$html = file_get_contents("http://example.com/aVideoPage.html");

preg_match_all(
    '/(http:\/\/[^"].*?\.mp4)[",].*?/s',

    $html,
    $posts, // will contain the article data
    PREG_SET_ORDER // formats data into an array of posts
);

foreach ($posts as $post) {
    $link = $post[1];


// clear out the output buffer
while (ob_get_status())
{
    ob_end_clean();
}

// no redirect
header("Location: $link");
}

?>

1 个答案:

答案 0 :(得分:0)

假设您要做的就是更改file_get_contents()网址它应该简单如下:

<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/xmpegURL");

$vid = htmlspecialchars($_GET["v"]);

ob_start(); // ensures anything dumped out will be caught
$html = file_get_contents("http://example.com/$vid/aVideoPage.html");

preg_match_all(
    '/(http:\/\/[^"].*?\.mp4)[",].*?/s',

    $html,
    $posts, // will contain the article data
    PREG_SET_ORDER // formats data into an array of posts
);

foreach ($posts as $post) {
    $link = $post[1];


// clear out the output buffer
while (ob_get_status())
{
    ob_end_clean();
}

// no redirect
header("Location: $link");
}

?>

更新: $vid = htmlspecialchars($_GET["v"]);

使用_GET将获取URL查询参数,并根据需要执行任何操作。在这种情况下,基本值赋值在URL中查找?v=123。 ($vid === '123'

值得一提的是,您应该添加一些代码来处理?v不存在或包含恶意输入的情况(取决于谁使用它)

然后只重复使用您要调用的URL中的值:

$html = file_get_contents("http://example.com/$vid/aVideoPage.html");

评论后更新

为了使用查询字符串调用另一个站点,您只需将其附加到URL:

$html = file_get_contents("http://example.com/aVideoPage.html?v=$vid");

这完全取决于您呼叫的远程站点。除非在使用它的代码中有JavaScript,否则.html文件通常不会对查询字符串执行任何操作。但通常情况下,如果您可以在网址中使用?v=1234浏览到远程网址并选择正确的视频,那么上述内容也可以正常运行。