PHP 从 shell_exec 获得一些零件

PHP get some parts from shell_exec

我需要你们的帮助我的服务器上有一些 php 代码 运行:

<?php
if ($_GET['id'])
$id = $_GET['id'];
$json = file_get_contents('http://www.youtubeinmp3.com/fetch/?format=JSON&video=http://www.youtube.com/watch?v=' . $id);
    $data = json_decode($json, true);
    $stream = $data['link'];
    $url    = $stream;



$output = shell_exec ('curl -I -L ' . $url);
echo "<pre>$output</pre>";
?>

当我转到:http://146.185.137.252/new.php?id=HgzGwKwLmgM

时它会输出这个

它输出这个:

HTTP/1.1 302 Moved Temporarily
Date: Sat, 11 Feb 2017 01:25:56 GMT
Content-Type: text/html
Connection: keep-alive
Set-Cookie: __cfduid=db002cf41b2472eb20de42ec65b16029a1486776356; expires=Sun, 11-Feb-18 01:25:56 GMT; path=/; domain=.youtubeinmp3.com; HttpOnly
X-Powered-By: PHP/5.5.38
Location: //w23.youtubeinmp3.com/download/get/?id=HgzGwKwLmgM&r=kT7YFcbHgX3m25ULP3GeWulVx9OmT2qd&t=Queen+-+Don%27t+Stop+Me+Now+%28Official+Video%29
Server: cloudflare-nginx
CF-RAY: 32f40283e57d148b-AMS

HTTP/1.1 200 OK
Server: nginx
Date: Sat, 11 Feb 2017 01:25:56 GMT
Content-Type: audio/mpeg
Content-Length: 3499136
Last-Modified: Fri, 18 Nov 2016 18:09:22 GMT
Connection: keep-alive
Content-Disposition: attachment; filename="Queen - Don't Stop Me Now (Official Video).mp3"
Accept-Ranges: bytes
Expires: 0
ETag: "582f43d2-356480"
Accept-Ranges: bytes

但我不需要那些东西我只需要两件事: (Location:) 和 (Content-Disposition: attachment; filename=)

在两个不同的变量中所以在这种情况下我需要:

//w23.youtubeinmp3.com/download/get/?id=HgzGwKwLmgM&r=kT7YFcbHgX3m25ULP3GeWulVx9OmT2qd&t=Queen+-+Don%27t+Stop+Me+Now+%28Official+Video%29

Queen - 现在不要阻止我(官方视频)(顺便说一句,我不想​​要 .mp3)

我到处都在网上搜索过,但老实说,我完全是个菜鸟 所以如果有人能帮助我,那就太棒了

如果您只想要您在问题中所写的文件名和标题。

你可以像这样简单地使用它们

$link = $data['link'];
$title = $data['title'];

一旦你有了它,你可以打印它或像这样用它制作一个 link

echo "<a href=$link>$title</a>";

除非您想保存文件,否则不需要 curl(和 shell_Exec)部分

看起来像这样:

<?php
if (isset($_GET['id'])) {
  $id = $_GET['id'];
  $json = file_get_contents('http://www.youtubeinmp3.com/fetch/?format=JSON&video=http://www.youtube.com/watch?v=' . $id);
  $data = json_decode($json, true);

  $link = $data['link'];
  $title = $data['title'];

  echo "<a href=$link>$title</a>";
  }
?>  

此代码已经过测试。响应是从我的浏览器复制的。

我只是使用 curl 来执行 curl 请求并使用 curl_getinfo($ch,CURLINFO_REDIRECT_URL) 来获取重定向 link。

我还添加了获取 header 的功能,除非出现问题并且您希望看到响应 header.

PHP

<?php
header('content-type: text/plain');
$id='HgzGwKwLmgM';
$json = file_get_contents('http://www.youtubeinmp3.com/fetch/?format=JSON&video=http://www.youtube.com/watch?v=' . $id);
$json = json_decode($json, true);
$url = $json['link'];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
$header = curl_exec($ch);
$link = curl_getinfo($ch,CURLINFO_REDIRECT_URL);
echo "\nLINK:  $link\n\nHEADER: $header";

?>

响应:

LINK:  http://w23.youtubeinmp3.com/download/get/?id=HgzGwKwLmgM&r=aRDQ1MGdfkY3Mm6EFxQqEVqgd9FHf81q&t=Queen+-+Don%27t+Stop+Me+Now+%28Official+Video%29

HEADER: HTTP/1.1 302 Moved Temporarily
Date: Sun, 12 Feb 2017 16:23:39 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
Set-Cookie: __cfduid=d27df4bad722b0449b88b37d4b9d9b7341486916619; expires=Mon, 12-Feb-18 16:23:39 GMT; path=/; domain=.youtubeinmp3.com; HttpOnly
X-Powered-By: PHP/5.5.38
Location: //w23.youtubeinmp3.com/download/get/?id=HgzGwKwLmgM&r=aRDQ1MGdfkY3Mm6EFxQqEVqgd9FHf81q&t=Queen+-+Don%27t+Stop+Me+Now+%28Official+Video%29
Server: cloudflare-nginx
CF-RAY: 330162e8b07e115f-DFW