这是一个脚本,可以从youtube视频ID中直接获取mp3下载链接,变量$ gg是视频ID
因此,当我在本地xampp上运行此代码时,它运行正常并返回直接下载链接,但当我尝试在hosts server上运行此代码时,它会返回一个链接但是不是直接链接而是下载页面我做错了什么?
<?php
$gg = '6Y1Emb7Jyks';
$site = 'http://www.youtubeinmp3.com/widget/button/?video=https://www.youtube.com/watch?v='.$gg;
$html = file_get_contents($site);
$dom = new DOMDocument();
@$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//a");
for ($i = 0; $i < $hrefs->length; $i++) {
$href = $hrefs->item($i);
$url = $href->getAttribute('href');
}
$lol = 'http://www.youtubeinmp3.com/'.$url;
echo $lol;
答案 0 :(得分:0)
自己弄清楚我必须遵循重定向
最终代码
<?php
$gg = '6Y1Emb7Jyks';
$site = 'http://www.youtubeinmp3.com/widget/button/?video=https://www.youtube.com/watch?v='.$gg;
$html = file_get_contents($site);
$dom = new DOMDocument();
@$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//a");
for ($i = 0; $i < $hrefs->length; $i++) {
$href = $hrefs->item($i);
$url = $href->getAttribute('href');
}
$lol = 'http://www.youtubeinmp3.com/'.$url;
$url= $lol;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$a = curl_exec($ch);
$url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
echo $url;