PHP从shell_exec获取一些部分

时间:2017-02-11 01:35:49

标签: php curl terminal 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 + - +唐%27吨+停止+我+现在+%28Official +视频%29

女王 - 不要现在停止我(官方视频)(我不想要.mp3顺便说一句)

我到处搜索网络,但说实话,我是一个完全的菜鸟 所以,如果有人能帮助我,这将是惊人的

2 个答案:

答案 0 :(得分:0)

如果你只想要你在问题上写的文件名和标题。

你可以像这样使用它们

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

一旦你有了这个,你可以打印它或像这样链接它

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>";
  }
?>  

答案 1 :(得分:0)

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

我刚使用curl执行curl请求并使用curl_getinfo($ch,CURLINFO_REDIRECT_URL)获取重定向链接。

我还添加了你不需要的标题,除非有问题,你想看到响应标题。

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