如何在PHP中只获取json中的URL

时间:2016-02-08 07:54:42

标签: php json

如何获取JSON of Mention格式的数据 我想用这种形式的json获取数据 {"22":{"quality":"22","type":"video\/mp4","url":"http://
请指导我怎么做。
提前致谢

我已经尝试过此代码

$url = 'http://api.miyulasi.com/youtube/1.0.0/download.php?id=7lCDEYXw3mM';
$data = file_get_contents($url);
header('Content-Type: application/json');
echo $data;

现在回复就像这样

<div class="col-md-3 col-sm-3 col-xs-6 text-center downbuttonbox"><a href="http://redirector.googlevideo.com/videoplayback?

1 个答案:

答案 0 :(得分:2)

你去吧

<?php

$url = 'http://api.miyulasi.com/youtube/1.0.0/download.php?id=7lCDEYXw3mM';
$data = file_get_contents($url);

$doc = new DOMDocument();
@$doc->loadHTML($data);
$xpath = new DomXPath($doc);

$links = array();
$expression = "//a[contains(@class, 'btn btn-default btn-sm downbuttonstyle')]";
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, $quality);

    $links[] = array("itag" => $itag[1], "type" => $mime[1], "url" => $link, "quality" => $quality[1]);
}
header('Content-Type: application/json');
echo json_encode($links, JSON_PRETTY_PRINT);