如何从URL获取数据

时间:2016-04-05 08:15:23

标签: php

你好,这是我的代码,我无法获取数据,也不知道在哪里犯了错误。我试图在输出中获得下载链接。请更正此代码。

<?php
$id = 'yeyBEpsZfrs';

        $url = "http://youpak.com/watch?v=".$id;
        $html = file_get_contents($url);
        $dom = new DOMDocument('1.0', 'utf-8');
        $dom->loadHTML($html);

        $classname = 'btn-group btn-group-justified';
        $xpath = new DOMXPath($dom);
        $results = $xpath->query("//*[@class='" . $classname . "']");
        if ($results->length > 0) 
        {
        $children = $results->item(0)->childNodes;
            foreach ($children as $child) { 
                $ar['download'] .= $child->ownerDocument->saveXML( $child ); 
            } 
        }
        $belement = $dom->getElementById("videoholder");
        $dom->saveHTML($belement);
        $ar['play'] = $belement->C14N();
        return($ar);

?>

1 个答案:

答案 0 :(得分:1)

您可以使用此代码以JSON格式获取此数据

    $id = 'yeyBEpsZfrs';
    $url = "https://youpak.com/watch?v=".$id;
    $html = file_get_contents($url);
    $dom = new DOMDocument();
    @$dom->loadHTML($html);

    $classname = 'btn-group btn-group-justified';
    $xpath = new DOMXPath($dom);
    $results = $xpath->query("//*[@class='" . $classname . "']");
    if ($results->length > 0) 
    {
    $children = $results->item(0)->childNodes;
        foreach ($children as $child) { 
            @$ar['download'] .= $child->ownerDocument->saveXML( $child ); 
        } 
    }


$links = array();
$expression = "//a[contains(@class, '')]";
foreach($xpath->evaluate($expression) as $link) {
$content = $link->textContent;
$link = $link->getAttribute('href');
preg_match("/&mime=([^&]*)&/", $link, $mime);
preg_match("/&itag=([^&]*)&/", $link, $itag);
preg_match("/\((.*)\)/", $content, $extension);
@$links[$itag[1]] = array(
"quality" => $itag[1],
"type" => $mime[1],
"url" =>  $link 
);
}
header('Content-Type: application/json');
echo json_encode($links);